Troubleshooting Common Git Problems
No matter how carefully you work, Git errors are inevitable — especially when collaborating with others. Fortunately, most issues have simple solutions if you understand what’s going wrong.
Let’s explore the most common Git problems and how to fix them.
Problem 1: Merge Conflicts
Scenario: You and someone else edited the same line of a file or edited overlapping files. When merging branches, Git shows a conflict.
Symptoms:
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
Fix:
- Open the file Git mentioned.
- Find the conflict markers:
<<<<<<< HEAD
your changes
=======
their changes
>>>>>>> branch-name
3. Choose which version to keep, or combine both.
4. After editing:
git add index.html
git commit -m "Resolve merge conflict"
Problem 2: Accidentally Committed the Wrong File
Fix:
git reset HEAD filename
This unstages the file — it’s still in your working directory but not in the next commit.
To remove the file completely:
git rm --cached filename
Problem 3: Committed Too Early or Made a Mistake in the Message
Fix:
Edit the last commit message:
git commit --amend
You can update the message or add changes to the same commit. (Avoid this if you already pushed to a shared branch.)
Problem 4: Need to Undo the Last Commit
Fix:
If you haven’t pushed it yet:
git reset --soft HEAD~1
This undoes the last commit but keeps your changes.
To undo and erase the changes:
git reset --hard HEAD~1
⚠️ Use --hard
with caution! It permanently deletes changes.
Problem 5: Want to Discard Changes in a File
Fix:
git checkout -- filename
This reverts the file back to the last committed version (erasing current changes).
Problem 6: Permission Denied (SSH Error)
Symptoms:
Permission denied (publickey).
Fix:
You probably didn’t add your SSH key to GitHub.
- Generate an SSH key:
ssh-keygen -t ed25519 -C "your_email@example.com"
2. Add it to GitHub → Settings → SSH and GPG Keys → New SSH key
Problem 7: Pushed to the Wrong Branch
Fix:
- Create a new branch from that commit:
git checkout -b correct-branch
2. Push it:
git push origin correct-branch
3. Delete the mistaken push:
git push origin --delete wrong-branch
🧹 Tip: Use Git Status Often
git status
This is your best friend. It tells you:
- What’s modified
- What’s staged
- What’s untracked
- What branch you’re on
Summary: Common Problems & Fixes
Problem | Solution |
---|---|
Merge conflict | Manually resolve and commit |
Wrong file staged | git reset HEAD file |
Undo last commit | git reset --soft HEAD~1 |
Discard file changes | git checkout -- file |
SSH error | Add SSH key to GitHub |
Wrong branch push | Checkout new branch and delete wrong one |
Learning to troubleshoot is just as important as learning Git commands — and over time, you’ll fix issues faster and with more confidence.