Git is an essential tool for developers, enabling seamless version control and collaboration. Whether you're a beginner or a seasoned pro, this Git cheatsheet will help you navigate through essential commands with ease! ๐ก
๐ Table of Contents
- Getting Started with Git
- Basic Git Commands
- Branching and Merging
- Working with Remote Repositories
- Viewing History and Tracking Changes
- Advanced Git Commands
- Best Practices & Pro Tips
๐ Getting Started with Git
โ Install Git
Download and install Git from git-scm.com, then verify the installation:
git --version
๐ฏ Initialize a Repository
git init
Creates a new Git repository in the current directory.
๐ฅ Clone an Existing Repository
git clone <repository-url>
Copies a remote repository to your local machine.
๐ฅ Basic Git Commands
๐ง Check Repository Status
git status
Shows the current state of your working directory.
๐ Stage Changes
git add <file>
git add .
Adds files to the staging area (. stages all changes).
โ Commit Changes
git commit -m "Descriptive commit message"
Saves the staged changes to your local repository.
๐ ๏ธ Modify Last Commit
git commit --amend -m "Updated commit message"
Allows you to edit the last commit (only use before pushing!).
๐ฟ Branching and Merging
๐ฑ Create a New Branch
git branch <branch-name>
Creates a new branch.
๐ Switch Branches
git checkout <branch-name>
Switches to the specified branch. Alternatively, use:
git switch <branch-name>
๐ Create and Switch in One Step
git checkout -b <branch-name>
Creates and switches to a new branch.
๐ Merge Branches
git merge <branch-name>
Integrates changes from the specified branch into the current branch.
๐๏ธ Delete a Branch
git branch -d <branch-name>
Deletes a branch that has been merged. Use -D to force delete.
๐ Working with Remote Repositories
๐ View Remote Repositories
git remote -v
Lists the remote repositories linked to your project.
๐ Add a Remote Repository
git remote add origin <repository-url>
Links a local repo to a remote server.
๐ Fetch Updates
git fetch origin
Retrieves changes from the remote without merging.
โฌ๏ธ Pull Changes
git pull origin <branch-name>
Updates your local branch with remote changes.
โฌ๏ธ Push Changes
git push origin <branch-name>
Uploads your commits to the remote repository.
๐ Viewing History and Tracking Changes
๐ View Commit History
git log
Displays the commit history. Use:
git log --oneline --graph --decorate
for a compact view.
๐ Show Specific Commit Details
git show <commit-hash>
Displays details of a particular commit.
๐ฌ Compare Changes
git diff
Shows differences between working directory and staged files.
๐ Stash Changes Temporarily
git stash
git stash pop
Saves unfinished work without committing and restores it later.
๐ ๏ธ Advanced Git Commands
๐งน Rebase (Reapply Commits)
git rebase <branch-name>
Reapplies commits on top of another base.
๐ Reset to a Previous Commit
git reset --soft <commit-hash>
git reset --hard <commit-hash>
--soft keeps changes staged, --hard erases them.
โช Revert a Commit
git revert <commit-hash>
Creates a new commit that undoes a previous commit.
๐ Cherry-Pick Specific Commits
git cherry-pick <commit-hash>
Applies a specific commit from another branch.
๐ฏ Best Practices & Pro Tips
โ Commit Often & Use Meaningful Messages ๐
โ Use Feature Branches for New Changes ๐ฟ
โ
Regularly Sync with Remote (git pull) ๐
โ Resolve Merge Conflicts Carefully ๐ค
โ Keep Your Repository Clean & Organized ๐งน
๐ Conclusion
Mastering Git is essential for efficient software development. This Git cheatsheet serves as a quick reference for everyday commands and best practices. Keep practicing, experiment with different commands, and soon you'll be a Git expert! ๐
Happy coding! ๐จโ๐ปโจ
Top comments (0)