brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git merge --abort gets you out of a merge that ended in conflicts

Git
~/tips/tip-git-merge-abort

git merge --abort

Cancels a merge that stopped on conflicts and puts the branch back the way it was before.

You start a merge, Git lists a handful of conflicts, and you realise you picked the wrong branch or you simply do not have the time today. There is no need to clean up conflict markers by hand. There is a way back.

git merge --abort

The documentation describes it as aborting the current conflict resolution process and trying to reconstruct the pre-merge state. Here is a small case from a test repository, where both branches changed the same line of a file:

$ git merge left
Auto-merging c
CONFLICT (content): Merge conflict in c
Automatic merge failed; fix conflicts and then commit the result.
$ git status -sb
## main
UU c
$ git merge --abort
$ git status -sb
## main
$ cat c
R

After the abort, the status is clean and the file has the version from the branch you merge into again, without markers. The file name c and the contents are just the demo.

The caveat is in the same documentation. If you had uncommitted changes in the working tree when the merge started, git merge --abort will in some cases be unable to reconstruct them. The advice there is to commit or stash before you run git merge. That is a good habit anyway, and it costs one command.

If you want to keep the files exactly as they are but leave the merge state, the git merge -h summary lists a sibling, --quit. It is described there as --abort but leaving the index and working tree alone. I would only reach for it when you know what you are doing.

Rebase has the same escape hatch under the same name, git rebase --abort.

Further reading: git-merge documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git branch --edit-description [<branch-name>] All Git tips (144) All topics

Link