Sub topic 8
Backing out with git merge --abort
This chapter assumes you can create branches, commit, and read git status output. It also assumes you have seen a merge stop with a conflict, as in the earlier chapters. Here you will learn how to leave an unfinished merge safely, what Git restores when you do, and where the command can fail.
What an unfinished merge looks like
When git merge hits a conflict, it stops partway. Git has already changed files in your working tree and index (the staging area). It also records that a merge is under way by writing .git/MERGE_HEAD, which holds the commit you are merging in.
You are now in a special state. You can finish the merge by resolving every conflict and committing. You can also leave the merge and go back to where you started. git merge --abort does the second.
Reproduce a conflict first, using the setup from earlier in this tutorial:
git init -b main demo && cd demo
printf 'timeout = 10\n' > config.txt
git add config.txt && git commit -m "Add config"
git switch -c feature
printf 'timeout = 60\n' > config.txt
git commit -am "Raise timeout to 60"
git switch main
printf 'timeout = 30\n' > config.txt
git commit -am "Raise timeout to 30"
git merge featureGit reports CONFLICT (content): Merge conflict in config.txt and Automatic merge failed; fix conflicts and then commit the result. Run git status and you will see You have unmerged paths and a hint that includes git merge --abort.
Running the abort
git merge --abortThis abandons the merge and returns the branch and working tree to their state before git merge was run. After it runs:
.git/MERGE_HEADis gone, so Git no longer considers a merge to be in progress.- The index matches the last commit on your current branch (
HEAD). - Files touched by the merge are restored to their
HEADversions. - Your branch still points at the same commit as before. No history is lost, and the
featurebranch is untouched.
Check the result:
git status
cat config.txtgit status reports a clean working tree, and config.txt contains timeout = 30, your side's version. You can now run git merge feature again whenever you are ready.
When to abort
Aborting is a sound choice in several situations:
- You started the merge on the wrong branch. Abort, switch branches, and merge again.
- The conflicts are much larger than you expected. Abort, then look at what the two branches changed before deciding how to proceed.
- You realize the other branch is not ready, for example its tests fail.
- You made a mess of the resolution and want a clean restart.
- You want to merge differently, for example to rebase first so the conflicts are smaller.
Aborting costs nothing as long as the merge was the only thing you changed since your last commit. The next section covers the case where it was not.
The uncommitted changes caveat
The safest merge starts from a clean working tree. Git will often refuse to start a merge if uncommitted changes would be overwritten. But Git allows a merge to start when your uncommitted changes touch files the merge does not change. If that merge then conflicts, your own edits are mixed in with the merge state.
The manual page for git merge warns about this. git merge --abort is equivalent to git reset --merge when MERGE_HEAD is present. It tries to reconstruct the pre-merge state, but if there were uncommitted changes when the merge started, it may not be able to restore them. Treat that as a risk, not a guarantee.
Two habits keep you safe:
- Run
git statusbefore everygit mergeand confirm it saysnothing to commit, working tree clean. - If you must merge with local edits, stash them with
git stash pushand restore them withgit stash popafter the merge is done or aborted.
Abort versus other ways of backing out
Several commands can undo a merge, and they apply at different times.
| Situation | Command | Effect |
|---|---|---|
| Merge stopped with conflicts, not yet committed | `git merge --abort` | Returns to the pre-merge state. |
| Merge completed with a merge commit, not yet pushed | `git reset --hard ORIG_HEAD` | Moves the branch back to where it was before the merge. Discards uncommitted work. |
| Merge commit already shared with others | `git revert -m 1 <merge-commit>` | Adds a new commit that undoes the merge's changes. History is not rewritten. |
git merge --abort only works while the merge is unfinished. After you run git commit to complete the merge, MERGE_HEAD is removed and the command fails with an error such as fatal: There is no merge to abort (MERGE_HEAD missing). The same error appears if you run it in a repository where no merge was ever started. It is harmless, and it tells you which state you are in.
Be careful with git reset --hard ORIG_HEAD. It throws away uncommitted changes without asking. Use it only when you are sure you want the branch back where it was.
Aborting a rebase or a cherry-pick
git merge --abort only handles merges. Other operations that stop on conflicts have their own exit commands:
| Operation | Abort command |
|---|---|
| `git merge` | `git merge --abort` |
| `git rebase` | `git rebase --abort` |
| `git cherry-pick` | `git cherry-pick --abort` |
| `git revert` | `git revert --abort` |
Running the wrong one gives an error, and git status normally tells you which operation is in progress. A later chapter covers rebase conflicts in detail.
Common mistakes
- Closing the editor mid-merge and assuming it is over. The merge state persists in
.git. Rungit statuswhen you return. It reports that you are still merging. - Deleting
.git/MERGE_HEADby hand. This leaves the index in a conflicted state while Git forgets it was merging. Usegit merge --abortinstead. - Aborting after
git addand expecting your edits to stay. Abort discards resolution work, including edits you already staged. If you want to keep part of it, copy the file somewhere first. - Committing a half-resolved file. If you commit while conflict markers remain in a file, you will get a merge commit that contains
<<<<<<<lines. Abort is only available before that commit, so check for markers before you commit.
You can search for leftover markers before committing:
git diff --checkThis reports lines containing unresolved conflict markers and also whitespace problems, so it is a useful last check in the middle of a merge.
Restart after aborting
After an abort, the two branches are exactly as they were. A good restart sequence is:
git status
git log --oneline --graph --all
git diff main...featureThe first command confirms a clean tree. The second shows the shape of the history. The third, with three dots, shows what feature has changed since it diverged from main, which is the same set of changes the merge tried to apply. Reviewing it tells you where the conflicts will come from, and whether you want to merge, rebase, or ask the other author to coordinate.
Exercise
You ran git merge feature, got a conflict in config.txt, edited the file, and ran git add config.txt. Then you realize you merged the wrong branch. Can you still use git merge --abort, and what happens to your edit?
Show answer
Yes. The merge has not been committed, so MERGE_HEAD still exists and git merge --abort works. It returns the branch, index and working tree to the state before the merge, so your staged resolution of config.txt is discarded. If you want to keep the edited text, copy it elsewhere first. Because the tree was clean before the merge started, nothing else is lost.
Exercise
A teammate finished a merge with git commit and then says "just run git merge --abort". What happens, and what should you do instead if the merge commit has not been pushed?
Show answer
Git fails with "There is no merge to abort (MERGEHEAD missing)", because the merge is already complete. If the merge commit has not been pushed, run git reset --hard ORIGHEAD after checking there is no uncommitted work, which moves the branch back to its pre-merge commit. If it has been pushed, use git revert -m 1 on the merge commit instead.