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 revert -m 1 undoes a merge commit

Git
~/tips/tip-git-revert-mainline-merge

git revert -m 1 <merge-commit>

Undoes a merge commit by telling git which parent counts as the main line.

Reverting a normal commit is easy. Try it on a merge commit and git answers that the commit is a merge and you must say which parent to keep. That one number is the whole trick.

git revert -m 1 <merge-commit>

A merge has two parents. -m 1 says that the first parent is the mainline, which is the branch you merged into. Git then creates a new commit that reverses everything the merge brought in relative to that side. Look at the parents with git show --format=%p -s <merge-commit> if you are not sure which is which.

The manual has a warning that matters. Reverting a merge declares that you never want the changes the merge brought in, so later merges of the same branch only bring in commits that were not part of the reverted merge. If the branch is fixed and merged again, the old changes stay out. The usual way around that is to revert the revert first, and the revert-a-faulty-merge how-to in the git documentation walks through it.

The revert is a new commit and does not rewrite history, so it is safe on a branch other people use. Run git status and your tests before you push it, and expect conflicts if the code moved on since the merge.

Further reading: git revert, –mainline (Git 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 merge --abort All Git tips (144) All topics

Link