Common Git Errors and How to Fix Them

Test setup: Ubuntu 24.04 with Git 2.43.0, November 29, 2026. Each fix linked below says whether we reproduced that error.

Most Git errors come from a few situations: two histories that don’t line up, a push the remote won’t accept, a branch or remote name that doesn’t match, or a command run outside a repository. Find the line that starts with fatal: or error: and match it below.

Message What happened Fix
fatal: refusing to merge unrelated histories The two branches share no commit Refusing to merge unrelated histories
Updates were rejected because the tip of your current branch is behind The remote has commits you don’t Push rejected (non-fast-forward)
fatal: Not possible to fast-forward, aborting. Pull is set to fast-forward only and both sides moved Push rejected (non-fast-forward)
error: src refspec main does not match any You pushed a branch that doesn’t exist locally src refspec does not match any
Author identity unknown Git doesn’t know your name and email, so the commit failed Set user.name and user.email, see src refspec
error: remote origin already exists. You added a remote name that’s taken Remote origin already exists
You are in 'detached HEAD' state You checked out a commit, not a branch Detached HEAD
fatal: not a git repository No .git folder here or above Run Git from the project folder

Diagnose before you fix

Three commands answer most questions:

git status              # which branch you're on and what changed
git remote -v           # where push and pull go
git log --oneline --graph --all -n 20   # how the branches relate

If git log --graph shows two lines that never join, you have unrelated histories. If your branch is one or more commits behind origin/main, pull before you push.

Habits that prevent most Git errors

  • When you create a repo on GitHub for existing code, leave “Add a README” unticked, or clone it first and add your files.
  • Pull with an explicit strategy: git config --global pull.rebase false (merge) or true (rebase). Without a setting, Git 2.43 stops with “Need to specify how to reconcile divergent branches” when both sides have new commits.
  • Use git push --force-with-lease, never plain --force, on shared branches.
  • Run Git commands from the project folder. git rev-parse --show-toplevel shows the repo root.

Every Git error we cover

Merge and history errors

Push and remote errors

FAQ

How do I undo my last Git commit?

If you haven't pushed it, git reset --soft HEAD~1 removes the commit and keeps your changes staged. If you have pushed it, use git revert HEAD so you don't rewrite history other people have.

What does git reflog do?

It lists every position your branch tips and HEAD have pointed to recently, including commits that no branch shows any more. It's the usual way to find work after a bad reset or rebase.

Is it safe to force push?

Only on a branch nobody else uses. Prefer git push --force-with-lease, which refuses to overwrite commits you haven't seen.