Video in progress
A feature branch was merged into `main`, then the merge commit was reverted because of a problem. After fixing the bug, when you merge the branch again, the old changes do not come. Why?
The revert removed the code, but in the history the branch's commits are already counted as part of main. On a second merge Git takes only the new commits. You first need to revert the revert commit, then merge the new fixes.
📖 Answer
The history looks like this:
main: ---M---R--- (M is the merge, R is the revert of M)
/
feature: A---B---C---D (D is the new fix)
For Git, A, B and C are already ancestors of main. On the second merge it takes what comes after the merge-base, that is, only D. Meanwhile R is removing the code of A-B-C, so you end up with only half a feature on main.
The right order:
git switch main
git revert <R-hash> # "revert of revert": brings back the code of A-B-C
git merge feature # now only D comes in
An alternative: rewrite the feature commits onto a new base (git rebase --no-ff, or a cherry-pick onto a new branch). Then they become commits with new hashes that Git has "not seen", and a plain merge takes everything. But this duplicates the history.
This is also related to why reverting a merge commit itself needs -m 1: Git has to know which parent to go back to.
To avoid such situations in prod, many teams switch a large feature off with a feature flag instead of reverting it, and the Git history stays intact.
Sign up to mark studied questions and save your progress.