Git is an essential tool for every developer, enabling efficient version control and collaboration. Here’s a list of the most important Git commands you should master.
1. Git Configuration
Before you start using Git, configure your username and email:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Check your configuration with:
git config --list
2. Initializing and Cloning Repositories
To start a new Git repository:
git init
To clone an existing repository:
git clone https://github.com/user/repository.git
3. Staging and Committing Changes
Check the status of your repository:
git status
Stage files for commit:
git add filename # Add a specific file
git add . # Add all changes
Commit changes:
git commit -m "Your commit message"
4. Branching and Merging
Create a new branch:
git branch new-branch
Switch to the new branch:
git checkout new-branch
Alternatively, create and switch in one command:
git checkout -b new-branch
Merge a branch into the main branch:
git checkout main
git merge new-branch
Delete a branch:
git branch -d new-branch
5. Working with Remote Repositories
To add a remote repository:
git remote add origin https://github.com/user/repository.git
Push changes to a remote repository:
git push origin branch-name
Fetch changes from a remote repository:
git fetch origin
Pull changes and merge:
git pull origin branch-name
6. Undoing Changes
To undo changes in a file before staging:
git checkout -- filename
To remove a file from staging:
git reset filename
To undo the last commit (without losing changes):
git reset --soft HEAD~1
To undo the last commit (discard changes):
git reset --hard HEAD~1
7. Checking History and Logs
View commit history:
git log
View commit history in a single line:
git log --oneline --graph --decorate
Check the last commit:
git show HEAD
8. Stashing Changes
If you need to save changes temporarily without committing:
git stash
To apply stashed changes:
git stash pop
To list all stashes:
git stash list
9. Resolving Merge Conflicts
When merging branches, you may encounter conflicts. Use:
git status
Edit the conflicting files, then add and commit them:
git add .
git commit -m "Resolved merge conflict"
10. Deleting Files and Commits
To remove a file and commit the deletion:
git rm filename
git commit -m "Deleted filename"
To delete the last commit:
git reset --hard HEAD~1
11. Amending the Last Commit (Modify Commit Without Creating a New One)
git commit --amend -m "Updated commit message"
✅ Use this when you need to change the last commit’s message or include new changes without creating a new commit.
12. Finding and Fixing a Bad Commit (Bisecting)
git bisect start
git bisect bad # Mark current commit as bad
git bisect good <commit_hash> # Mark an older commit as good
✅ Helps in debugging when you need to find the exact commit that introduced a bug. Git will guide you through a binary search of commits.
13. Cherry-Picking Specific Commits
git cherry-pick <commit_hash>
✅ Use when you need to apply a specific commit from one branch to another without merging everything.
14. Recovering Deleted Branches
git reflog
git checkout -b recovered_branch <commit_hash>
✅ If you accidentally delete a branch, git reflog
helps you find its last commit and recover it.
15. Squashing Multiple Commits Into One
git rebase -i HEAD~3
✅ Great for cleaning up your commit history before merging a feature branch.
16. Temporarily Shelving Changes (Saving Work Without Committing)
git stash push -m "Work in progress"
git stash list
git stash pop
✅ Ideal when you need to switch branches quickly without committing incomplete work.
17. Undoing a Pushed Commit (Without Affecting Others)
git revert <commit_hash>
git push origin main
✅ Creates a new commit that undoes the changes of a specific commit, keeping history intact.
18. Clearing Local Commits Before Pushing
git reset --soft HEAD~3 # Keeps changes
git reset --hard HEAD~3 # Discards changes
✅ Use --soft
to uncommit but keep changes or --hard
to erase them completely.
19. Forcing a Pull to Overwrite Local Changes
git fetch --all
git reset --hard origin/main
✅ Use when you want your local branch to exactly match the remote branch, overwriting local changes.
20. Finding Who Made a Change to a Specific Line
git blame -L 42,42 filename.txt
✅ Helps track down who changed a specific line and when, useful for debugging and accountability.
These commands help developers handle tricky Git situations efficiently. Which of these have you used, or do you have any nightmare Git experiences? Let me know! 🚀
Conclusion
Mastering these Git commands will greatly improve your workflow, making you more efficient and productive. Start practicing these commands in your projects and become a Git pro!
Top comments (0)