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) ortrue(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-toplevelshows the repo root.
Every Git error we cover
Merge and history errors
- Fatal: Refusing to Merge Unrelated Histories (Git Fix)Git won't merge two branches that share no commit, often after you add a README on GitHub. When to use --allow-unrelated-histories and when to rebase instead.
- Detached HEAD in Git: What It Means and How to Fix ItDetached HEAD means you checked out a commit rather than a branch. How to save your work on a new branch, go back to main and recover commits you left behind.
Push and remote errors
- Src Refspec Main Does Not Match Any: How to Fix ItYou pushed a branch that doesn't exist locally, usually because there's no commit yet or your branch is called master. Two commands fix it, tested on Git 2.43.
- Git Push Rejected (Non-Fast-Forward): How to Fix ItThe remote branch has commits you don't have. Pull with rebase, then push. When force push is safe, and when it would overwrite someone else's commits.
- Fatal: Remote Origin Already Exists: How to Fix It in GitYou tried to add a remote named origin when one already exists. Change its URL with git remote set-url origin, or rename or remove the old remote first.
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.
