💡 Ask Tutor

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:

  1. Open the file Git mentioned.
  2. Find the conflict markers:
Markdown
<<<<<<< HEAD
your changes
=======
their changes
>>>>>>> branch-name

3. Choose which version to keep, or combine both.

4. After editing:

Bash
git add index.html
git commit -m "Resolve merge conflict"

Problem 2: Accidentally Committed the Wrong File

Fix:

Bash
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:

Bash
git rm --cached filename

Problem 3: Committed Too Early or Made a Mistake in the Message

Fix:
Edit the last commit message:

Bash
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:

Bash
git reset --soft HEAD~1

This undoes the last commit but keeps your changes.

To undo and erase the changes:

Bash
git reset --hard HEAD~1

⚠️ Use --hard with caution! It permanently deletes changes.

Problem 5: Want to Discard Changes in a File

Fix:

Bash
git checkout -- filename

This reverts the file back to the last committed version (erasing current changes).

Problem 6: Permission Denied (SSH Error)

Symptoms:

Java
Permission denied (publickey).

Fix:
You probably didn’t add your SSH key to GitHub.

  1. Generate an SSH key:
Bash
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:

  1. Create a new branch from that commit:
Bash
git checkout -b correct-branch

2. Push it:

Bash
git push origin correct-branch

3. Delete the mistaken push:

Bash
git push origin --delete wrong-branch

🧹 Tip: Use Git Status Often

Bash
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

ProblemSolution
Merge conflictManually resolve and commit
Wrong file stagedgit reset HEAD file
Undo last commitgit reset --soft HEAD~1
Discard file changesgit checkout -- file
SSH errorAdd SSH key to GitHub
Wrong branch pushCheckout 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.

📘 Next Topic

GitHub for Teams & Organizations →