Fixing Common Git Errors
Don't panic! Every Git error has a solution. This guide covers the most common Git problems developers face in 2025 and how to fix them safely.
Before You Start: Safety First
Before attempting any fix, remember these golden rules:
- Git rarely loses data. Your work is probably recoverable.
- Use
git statusto understand the current state. - When in doubt, create a backup:
git branch backup-branch - The reflog is your friend:
git reflog
Common Git Errors & Solutions
Error: Merge Conflict
CONFLICT (content): Merge conflict in app.js
Automatic merge failed; fix conflicts and then commit the result.
Why This Happens
Git can't automatically determine which changes to keep when the same lines were modified in both branches. This is actually Git protecting you from losing work!
Step-by-Step Solution
1. See which files have conflicts:
git statusFiles with conflicts will be listed under "Unmerged paths"
2. Open the conflicted file. You'll see markers like this:
<<<<<<< HEAD
const greeting = "Hello";
=======
const greeting = "Hi there";
>>>>>>> feature-branch• <<<<<<< HEAD = Your current branch's version
• ======= = Separator
• >>>>>>> feature-branch = Incoming branch's version
3. Resolve the conflict by editing the file:
// Choose one version, combine them, or write something new
const greeting = "Hello there"; // Combined bothRemove the conflict markers and keep the code you want.
4. Mark the conflict as resolved:
git add app.js5. Complete the merge:
git commit -m "Resolve merge conflict in app.js"Pro Tips
- • Use VS Code's merge conflict editor for visual resolution
- • Abort the merge if needed:
git merge --abort - • Use merge tools:
git mergetool
Error: I Need to Undo My Last Commit
Scenario: Wrong commit message or forgot to add files
Option 1: Undo commit but keep changes (Most Common)
git reset --soft HEAD~1Your changes stay staged. You can now fix them and commit again.
Option 2: Undo commit and unstage changes
git reset HEAD~1Changes go back to the working directory (unstaged). Good if you want to review before re-committing.
Option 3: Fix the commit message only
git commit --amend -m "Fixed commit message"Keeps the changes but replaces the commit message.
Option 4: Add forgotten files to the last commit
git add forgotten-file.js\ngit commit --amend --no-editAdds new files to the previous commit without changing the message.
Warning
Never use --amend or reset on commits you've already pushed and shared with others. It rewrites history and will cause problems for your team.
Error: Detached HEAD State
You are in 'detached HEAD' state. You can look around, make experimental changes...
but they will be orphaned when you checkout another branch.
Why This Happens
You checked out a specific commit instead of a branch. Any new commits won't belong to any branch and might be lost when you switch branches.
Solutions
If you haven't made changes yet:
git checkout main\n# or\ngit switch mainJust go back to your branch. No harm done.
If you made commits in detached HEAD:
# Create a new branch from here\ngit branch my-detached-work\ngit checkout my-detached-work\n\n# Now merge it into main if needed\ngit checkout main\ngit merge my-detached-workError: Push Rejected (Non-Fast-Forward)
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'origin'
hint: Updates were rejected because the tip of your current branch is behind
Why This Happens
Someone else pushed changes to the remote branch before you did. Git won't let you overwrite their work.
Solution
1. Pull the remote changes first:
git pull origin mainThis will merge the remote changes with your local commits.
2. If there are merge conflicts, resolve them (see above)
3. Push again:
git push origin mainAlternative: Rebase instead of merge (cleaner history):
git pull --rebase origin main\n# Resolve any conflicts\ngit push origin mainNever Do This
git push --force will overwrite others' work. Only use it on your own branches, never on shared branches like main.
Error: I Lost My Commits (Accidentally Reset/Deleted)
Don't Panic! Git Reflog to the Rescue
Git keeps a log of every HEAD movement for 30+ days. Your commits are probably still there.
1. View the reflog:
git reflogThis shows all recent actions. Look for the commit you lost.
2. Find your lost commit in the output:
abc1234 HEAD@{0}: reset: moving to HEAD~1
def5678 HEAD@{1}: commit: My important commit ← This one!
ghi9012 HEAD@{2}: commit: Previous commit3. Recover it:
git checkout def5678\n# Or create a branch from it\ngit branch recovered-work def56784. Merge it back to main:
git checkout main\ngit merge recovered-workPro Tip
The reflog is local to your machine. If you haven't pushed your commits, they only exist in your local reflog. Always push important work!
Error: I Committed to the Wrong Branch
Scenario: You committed to main instead of a feature branch
1. Create a new branch with your commits:
git branch feature-branchThis creates a new branch pointing to your current commit. Your work is now safe!
2. Move main back to where it was:
git reset --hard origin/mainThis resets main to match the remote, removing your commits from main.
3. Switch to your feature branch:
git checkout feature-branchYour commits are safe on the feature branch!
Error: Permission Denied (publickey)
Permission denied (publickey).
fatal: Could not read from remote repository.
Why This Happens
Git can't authenticate with the remote server (GitHub, GitLab, etc.) because your SSH key isn't set up or recognized.
1. Check if you have an SSH key:
ls -al ~/.sshLook for id_rsa and id_rsa.pub (or id_ed25519 and id_ed25519.pub)
2. If no SSH key exists, create one:
ssh-keygen -t ed25519 -C "your_email@example.com"3. Add the key to your SSH agent:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed255194. Copy your public key:
cat ~/.ssh/id_ed25519.pubCopy the entire output and add it to your GitHub/GitLab account settings under SSH keys.
5. Test the connection:
ssh -T git@github.comQuick Error Recovery Commands
Check Status
git statusView Recent Actions
git reflogAbort Merge
git merge --abortAbort Rebase
git rebase --abortUndo Last Commit (Keep Changes)
git reset --soft HEAD~1Discard Local Changes
git restore file.txt