I’m having this strange problem with Git merging that I’m unable to explain or categorize. Is it a missing commit. Is it a merge gone wrong? Is it corrupted data? Here’s what the repository history looks like:
master----\----commit A----cherry-picked changesets from topic---commit B--\----commit C----merge---
\ \ /
topic-----------------------------------------------------------merge---------/
Now, my problem is that when master is merged INTO the topic branch (to bring it up-to-date with commits A & B), the changeset introducted by commit B is just not there! If commit B was modifying files foo & bar, even got lot does not show those files being changed with the merge. There isn’t even any conflict in files foo and bar
Now when I merge topic back into master, commit B is in-effect reversed without ANY log or trace of the reversal!
What could’ve gone wrong?
There is a merge already in topic that has commit B as parent. So B is fully merged in topic and won’t be merged anywhere anymore.
Since you don’t have the changes in topic, you apparently reverted them on topic, either in the merge itself or in a following commit. Such reversal is a regular commit for the merge algorithm and it’s not merged into master. So when you merge topic into master, this commit’s changes will be merged, reverting commit B.
To get the changes from B back, you have to either:
git cherry-pick B) on topic.How the changes might have been reversed without you realizing it? If you are merging and get conflicts, you might resolve them sloppily using “local” thinking that you don’t need these changes there yet. But from Git’s (or any other version control system’s for that matter; 3-way merge works the same in all of them) point of view you’ve seen the changes and rejected them, so you won’t get them again, ever, unless you manually re-apply them.
The conflict might have easily been caused by the earlier cherry-picks. While the algorithm won’t declare conflict if both sides look the same and thus if you cherry-pick and than merge, it will declare conflict if you modify the cherry-picked code on one side. Say you have:
where
B'picksBandB2modifies the same code thatBdid. In that case the merge will see that one side didBand the other side didB2, because the cherry-pick is hidden byB2and will thus declare conflict betweenBandB2. And if you don’t carefuly look at the history, you may easily resolve this wrong. You can avoid the problem if when picking a commit you carefuly merge the target branch into the source one like this:where
m1is normal merge with no cherry-pick involved andm2is resolved with local version, because it only has the cherry-picked changes on remote. That will ensure further merges will work correctly.It should actually be possible to write a merge strategy for git to do this automatically.