Sub topic 8
Backing out with git merge --abort
This chapter assumes you can create branches, commit, and read git status output. You should also know what a conflict looks like in a file. Here you will learn how to abandon an unfinished merge and get back to a clean state.
When to abort
A merge that stops with a conflict is not an error you must fix right away. Git leaves the repository in a "merging" state and waits. You can resolve the conflict, or you can decide the merge was a bad idea and undo it. Common reasons to back out:
- You merged the wrong branch.
- You merged into the wrong branch.
- The conflicts are larger than you expected, and you want to talk to the author of the other branch first.
- You started resolving and made a mess of the files.
git merge --abort handles all of these. It discards the merge attempt and returns your branch and working tree to the state they had before you ran git merge.
Abort a conflicted merge
The setup below recreates the conflict from earlier chapters. Both branches change the same line after their common commit.
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 a conflict in config.txt. Now back out:
git merge --abortAfter this command, git status shows a clean working tree. config.txt again contains timeout = 30, and main points at the same commit as before the merge. No merge commit was created, and feature is untouched.
What abort restores
Abort is designed to restore the pre-merge state, and it does so reliably when you started the merge from a clean working tree. Git can rebuild the original state because it remembers the commit you were on in a file called MERGE_HEAD and its related bookkeeping. The abort command also removes that bookkeeping, so git status no longer says "You have unmerged paths."
Your edits made while resolving, such as removed conflict markers, are thrown away too. Aborting means starting over.
Common errors
- "There is no merge to abort (MERGE_HEAD missing)." No merge is in progress. Either you already finished or aborted it, or the conflict came from a different operation such as a rebase. For a rebase, use
git rebase --abort. - Aborting after you committed. Once you run
git committo finish the merge, there is nothing to abort. To undo a completed merge, checkgit logfirst and then usegit reset --hard ORIG_HEADright after the merge, orgit revert -m 1 <merge-commit>if you already shared it. Both are different tools with different consequences, so do not mix them up with--abort. - Expecting abort to keep partial work. If you resolved three of five files and want to keep that work, abort will discard it. Finish the merge instead, or copy the files elsewhere first.
Restart the merge
After aborting, the branches are exactly as they were, so you can try again. You might merge in the other direction, update your branch first, or run the same merge with a different plan.
git merge --abort
git status
git merge featureExercise
You ran git merge feature and it stopped with conflicts in three files. You have resolved one of them and then realize you are on the wrong branch. What command returns the repository to its state before the merge, and what happens to the file you already resolved?
Show answer
Run git merge --abort. It restores the branch and working tree to the pre-merge state and removes the merge bookkeeping. The edits you made to the resolved file are discarded, so you must redo them if you merge again on the correct branch.
Exercise
You run git merge --abort and Git answers "There is no merge to abort (MERGE_HEAD missing)." Name two possible explanations.
Show answer
The merge was already completed or aborted, so no merge is in progress. Or the conflict came from another operation, such as a rebase, which needs git rebase --abort instead.