Common Git Errors: Branches and Merges
Branch and merge errors are the messages Git prints when switching branches, merging or naming files would lose work or doesn't make sense. Git is careful by design: almost every one of these errors stops before anything changes, to protect uncommitted work or commits that exist nowhere else. This page shows each one exactly as Git prints it, from real runs, with what it means, the fix, and how to avoid it.
A careful librarian is a good picture. Before reshelving your desk, they stop and ask about the open notebook on it. The question is annoying for a second, and it saves your notes.
The Starting Point
mkdir fee-tracker
cd fee-tracker
git init --quiet
printf "%s\n" 'Term fee: $12,000' 'Late fee: $250' > fees.txt
git add fees.txt
git commit --quiet -m "Add the fee list"
git switch --quiet -c feature/late-fee
sed -i.bak 's/\$250/$300/' fees.txt && rm fees.txt.bak
git commit --quiet -am "Raise the late fee"
git switch --quiet main Your Local Changes Would Be Overwritten
Navika edits fees.txt on main and, before committing, tries to switch to the feature branch, where the same file is different:
sed -i.bak 's/\$12,000/$12,500/' fees.txt && rm fees.txt.bak
git switch feature/late-feeerror: Your local changes to the following files would be overwritten by checkout:
fees.txt
Please commit your changes or stash them before you switch branches.
AbortingWhat it means: switching would replace fees.txt with the branch's version and throw away her uncommitted change, so Git refused and changed nothing. The same message appears for git pull, git merge, git rebase and git checkout when incoming changes touch files you have edited.
The fix: keep the change somewhere first. Commit it, stash it, or bring it along:
git stash push --quiet -m "Term fee idea"
git switch --quiet feature/late-fee
git stash list
git switch --quiet main
git stash pop --quiet
git status --shortstash@{0}: On main: Term fee idea
M fees.txtExplanation: git stash put the change aside (see "Git Stash"), the switch worked, and back on main git stash pop restored it. Other choices: git switch --merge feature/late-fee tries to carry the change across, and git switch --discard-changes throws it away on purpose. Navika commits it:
git commit --quiet -am "Raise the term fee" Avoid it: commit or stash before switching. git config --global rebase.autoStash true and git pull --autostash stash and restore automatically.
![Diagram: three ways past the error "Your local changes would be overwritten". Git refused to switch, pull or merge because a file you edited would change. Keep the work as a commit: git commit, then retry. Put it aside: git stash, retry, then git stash pop. Throw it away: git restore
You Are in 'Detached HEAD' State
git checkout "main~1"Note: switching to 'main~1'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 5e10e3e Add the fee listThis one isn't really an error, but it surprises people. git checkout <commit> prints the long explanation; git switch --detach <commit> does the same quietly.
What it means: HEAD points directly at a commit instead of a branch (see "HEAD and Detached HEAD"). You can look around and even commit, but new commits belong to no branch and are easy to lose when you switch away.
The fix: to keep work made here, create a branch: git switch -c <new-branch>. To leave, switch back to a branch:
git switch -Previous HEAD position was 5e10e3e Add the fee list
Switched to branch 'main'git switch - goes back to the previous branch. If you already left commits behind, find them with git reflog (see "Git Reflog").
Merge Conflict in a File
git switch --quiet feature/late-fee
sed -i.bak 's/\$12,000/$13,000/' fees.txt && rm fees.txt.bak
git commit --quiet -am "Raise the term fee more"
git switch --quiet main
git merge feature/late-feeAuto-merging fees.txt
CONFLICT (content): Merge conflict in fees.txt
Automatic merge failed; fix conflicts and then commit the result.What it means: both branches changed the same lines (the term fee) differently, and Git can't choose (see "How Git Merges"). The merge is paused: other changes are combined, and the file has conflict markers.
The fix: edit the file, keep the right content, remove the markers, then git add and commit. Or cancel:
git status --short
git diff --name-only --diff-filter=U
git merge --abort
git status --shortUU fees.txt
fees.txtExplanation: UU and --diff-filter=U list the conflicted files. git merge --abort returned everything to the state before the merge. The full resolution steps are in "Resolving Merge Conflicts". The same kind of conflict during a rebase, cherry-pick or stash pop is fixed the same way, then finished with --continue (or git stash drop after a conflicted pop).
Avoid it: merge or rebase from main often, keep branches short-lived, and agree on who changes shared files.
Pathspec Did Not Match Any Files
git add fee.txt
git switch feature/late-feesfatal: pathspec 'fee.txt' did not match any files
fatal: invalid reference: feature/late-feesWhat it means: a pathspec is a file name or pattern. Git found no file called fee.txt (the file is fees.txt) and no branch called feature/late-fees (the name is feature/late-fee). For git switch, Git calls it an invalid reference instead. On Linux, case matters too: Fees.txt is a different name from fees.txt; macOS and Windows usually ignore case.
The fix: check the real names with git status, git ls-files or git branch -a, and use tab completion. If the branch exists only on the server, run git fetch first; git switch <name> then creates it from origin/<name> (see "Pushing and Pulling Branches").
The Branch Is Not Fully Merged
git branch -d feature/late-feeerror: the branch 'feature/late-fee' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D feature/late-fee'
hint: Disable this message with "git config set advice.forceDeleteBranch false"What it means: the branch has commits that aren't in the current branch, so deleting it would make them hard to find. Git refuses with -d.
The fix: merge it first if the work is wanted. If you really want to throw it away, use -D (force):
git branch -D feature/late-feeDeleted branch feature/late-fee (was 36f8bc1).The message includes the commit ID, so even after -D you can bring it back with git branch feature/late-fee <id> (or find it in git reflog).
Cannot Delete or Use a Branch That Is Checked Out
git branch -d main
git branch feature/late-fee
git branch feature/late-feeerror: cannot delete branch 'main' used by worktree at '/home/navika/projects/fee-tracker'
fatal: a branch named 'feature/late-fee' already existsWhat it means: you can't delete the branch you are on, or one checked out in another worktree (see "Git Worktree"). And a branch name can exist only once.
The fix: switch to another branch first, or pick a different name. git branch -m old new renames a branch, and git switch -C <name> resets an existing branch to the current commit, on purpose.
Common Mistakes
Using git checkout -- . or git reset --hard to get past "would be overwritten". It deletes your uncommitted changes permanently. Stash first.
Committing with conflict markers still in the file. Search for <<<<<<< before committing; git diff --check warns about leftover markers.
Forcing a delete with -D without reading the commit ID. Note it, in case you need the branch back.
Typing names by hand. Tab completion avoids most "pathspec" and "invalid reference" errors.
Under the Hood
Before git switch or git merge updates the working folder, Git compares three things for every file it must change: the index, the working file and the new version. If a file it needs to overwrite has changes that aren't in any commit, it stops with "would be overwritten" and leaves everything as it was. That check happens before any file is written, which is why a refused command never leaves a half-switched folder. git branch -d checks whether the branch's tip is an ancestor of HEAD (or of its upstream, if it has one); if not, the branch is "not fully merged". A detached HEAD is simply a .git/HEAD file containing a commit ID instead of ref: refs/heads/<branch>.
Interview Corner
Git says "Your local changes to the following files would be overwritten by checkout". What do you do?
Save the changes first: commit them, or git stash, then switch and git stash pop later. Don't discard them with reset --hard unless you really mean to.
How do you get out of detached HEAD without losing work?
If you made commits, create a branch for them with git switch -c <name>. Otherwise just switch to a branch, for example git switch main.
What does "error: The branch 'x' is not fully merged" mean?
The branch has commits that aren't in the current branch. Merge it, or delete it anyway with git branch -D if the work isn't needed.
How do you cancel a merge with conflicts?
git merge --abort returns the branch, the index and the working folder to how they were before the merge.
Practice
Challenge: bring the branch back. Navika deleted the original feature/late-fee with -D earlier, and then created a new branch with the same name. Find the old tip in the reflog (the commit "Raise the term fee more"), move the branch back to it, and print its log.
What does Git print? Before you open the solution, decide why the reflog still knows the commit after the branch was deleted.
Show solution
old=$(git reflog --format=%h --grep-reflog="commit: Raise the term fee more")
git branch --force feature/late-fee "$old"
git log --oneline feature/late-fee36f8bc1 (feature/late-fee) Raise the term fee more
d45f2a4 Raise the late fee
5e10e3e Add the fee listThe branch's own reflog was deleted with it, but HEAD's reflog recorded every commit made while it was checked out, including "Raise the term fee more". --grep-reflog finds that entry, and git branch --force moves the existing branch name to it (see "Git Reflog").
Checkpoint. Reproduce five errors from this page in a practice repository, fix each one without losing work, and write down the command that fixed it.
Key Takeaways
Quick reference
| Error | Fix |
|---|---|
Your local changes ... would be overwritten | git stash, then switch, then git stash pop |
You are in 'detached HEAD' state | git switch -c <name> or git switch <branch> |
CONFLICT (content): Merge conflict in <file> | Resolve, git add, commit; or git merge --abort |
pathspec '<x>' did not match any file(s) | Check the name with git status or git branch -a |
The branch '<x>' is not fully merged | Merge it, or git branch -D <x> |
cannot delete branch '<x>' used by worktree | Switch away first |
Was this tutorial helpful?