This error occurs when Git detects an incomplete merge operation in your repository. A previous merge was started but never finished - either due to unresolved conflicts or a forgotten commit. Fix by completing the merge with a commit, aborting the merge, or manually removing the MERGE_HEAD file.
When you run a merge command in Git, it creates a special file called `.git/MERGE_HEAD` that tracks the commit being merged into your current branch. This file signals to Git that a merge operation is in progress. The "MERGE_HEAD exists" error occurs when Git detects this file while you're trying to perform another operation (like checkout, pull, or another merge). Git is telling you: "Hey, you started a merge but never finished it. Please complete that first." This typically happens in one of these scenarios: 1. **Merge conflicts occurred**: You started a merge, conflicts were detected, and you resolved them but forgot to run `git commit` to finalize the merge. 2. **Merge was interrupted**: You started a merge and then got distracted, closed your terminal, or switched to another task without completing it. 3. **Partial conflict resolution**: You resolved some conflicts and staged the files with `git add`, but never committed the merge result. Until you either complete the merge (by committing) or abort it, Git will block most operations to prevent you from creating an inconsistent repository state.
First, understand the current state of your repository:
git statusThis will show you one of several possible states:
If all conflicts are resolved:
On branch main
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)If conflicts still exist:
On branch main
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
both modified: filename.txtThis status tells you exactly what needs to be done next.
If you want to keep the merge changes, complete it by committing:
If all conflicts are already resolved:
git commit -m "Merge branch 'feature-branch' into main"Git may open your default editor for a merge commit message. Save and close to complete.
If conflicts still need resolution:
1. Open each conflicting file and resolve the conflicts (look for <<<<<<<, =======, and >>>>>>> markers)
2. After resolving each file, stage it:
git add <resolved-file>3. Once all conflicts are resolved, commit:
git commit -m "Merge branch 'feature-branch' into main"Using git merge --continue:
If you've already resolved all conflicts and staged the files, you can also use:
git merge --continueThis is equivalent to running git commit and will finalize the merge.
If you want to cancel the merge and return to the state before it started:
git merge --abortThis command will:
- Remove the MERGE_HEAD file
- Reset your working directory to the state before the merge
- Discard any conflict resolutions you made
If git merge --abort fails:
Sometimes you may see: "Entry 'filename' not uptodate. Cannot merge."
In this case, first stage your changes:
git add .
git merge --abortAlternative - git reset:
For older Git versions (before 1.7.4), use:
git reset --mergeThis achieves the same result as git merge --abort.
If the above methods don't work, you can perform a hard reset:
Warning: This will discard ALL uncommitted changes in your working directory.
git reset --hard HEADThis resets your branch to the last commit, removing the merge state and any uncommitted changes.
If you need to reset to a specific commit:
# View recent commits
git log --oneline -5
# Reset to a specific commit
git reset --hard <commit-hash>Last resort - manually remove MERGE_HEAD:
If Git commands are still not working, you can manually delete the merge state files:
rm -f .git/MERGE_HEAD
rm -f .git/MERGE_MSG
rm -f .git/MERGE_MODEAfter this, run git status to verify the merge state is cleared. Note that this may leave your working directory in an inconsistent state, so review your files carefully.
After completing or aborting the merge, verify everything is working:
# Check status - should show clean working tree
git status
# Verify MERGE_HEAD is gone
ls .git/MERGE_HEAD 2>/dev/null || echo "MERGE_HEAD cleared"
# Test that other operations work
git log --oneline -3You should now be able to:
- Checkout other branches
- Pull from remote
- Start new merges
- Perform other Git operations normally
### Understanding Git's Merge State Files
When a merge is in progress, Git creates several files in the .git directory:
| File | Purpose |
|------|---------|
| MERGE_HEAD | Contains the SHA of the commit being merged |
| MERGE_MSG | Pre-populated merge commit message |
| MERGE_MODE | Contains merge mode flags (if any) |
These files together tell Git that a merge is in progress. They are automatically removed when the merge completes or is aborted.
### Merge vs Rebase State
A similar error can occur during a rebase operation. If you see REBASE_HEAD instead of MERGE_HEAD, use:
git rebase --abort
# or
git rebase --continue### Recovering Lost Work
If you accidentally aborted a merge and lost conflict resolutions:
# Check reflog for recent HEAD positions
git reflog
# You may be able to recover to a previous state
git reset --hard HEAD@{1}### Git GUI Tools
If you use a GUI tool like VS Code, GitKraken, or Sourcetree:
1. The tool should show a "merge in progress" indicator
2. Use the tool's "Commit" or "Complete Merge" button
3. Or use the "Abort Merge" option in the menu
### Preventing This Error
To avoid this situation in the future:
1. Always check `git status` after resolving conflicts
2. Don't walk away from an active merge without completing it
3. Use `git stash` if you need to switch tasks mid-merge
4. Set up merge conflict markers in your editor for easy identification
### Handling Repeated Merge Conflicts
If you frequently encounter the same conflicts when merging:
# Enable rerere (Reuse Recorded Resolution)
git config --global rerere.enabled trueThis makes Git remember how you resolved conflicts and automatically apply the same resolution in the future.
warning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server