22

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?

nulltoken
  • 64,429
  • 20
  • 138
  • 130
Saurabh Nanda
  • 6,373
  • 5
  • 31
  • 60
  • Yes, there is a trace of the reversal. You had to do it. Manually. But you might have not been aware that you did. – Jan Hudec Nov 20 '12 at 08:07

2 Answers2

12
master----\----commit A----cherry-picked changesets from topic---commit B--\----commit C----merge---
           \                                                                \              /
            topic-----------------------------------------------------------merge---------/
                                                                            ~~~~~
                                                                              ^
                                                                              + here you merged B in topic

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:

  • Find and revert the reversal of B's changes on topic.
  • Cherry-pick B (git cherry-pick B) on topic.
  • Redo the merge, rebase topic after the merge on the new merge and forget the original branch, but as that involves rewinding you can only do that if you control all repositories that have the branch.

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:

----\-- A -------- B' -- B2 --\
     \                         \
      D -- B -- E -----------merge

where B' picks B and B2 modifies the same code that B did. In that case the merge will see that one side did B and the other side did B2, because the cherry-pick is hidden by B2 and will thus declare conflict between B and B2. 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:

----\-- A --------\- B' -\- B2 --\
     \             \      \       \
      D -- B -- E --m1-----m2----merge

where m1 is normal merge with no cherry-pick involved and m2 is 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.

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
  • But why B isn't applied the the merge you highlight in your answer? – VonC Nov 20 '12 at 08:10
  • @VonC: I suspect whoever was merging it got conflict and thought "I don't need this now" and resolved it with "mine". – Jan Hudec Nov 20 '12 at 08:12
  • @VonC: Of course if you think such mistake didn't happen, it might be useful to test that the merge worked correctly. It should be easy to recreate the merge situation by checking out that parent of the merge from topic and explicitly merge appropriate changeset from master and carefully review the result. – Jan Hudec Nov 20 '12 at 08:16
  • The explanation is plausible. But I am reluctant to favor a solution which add yet another cherry-pick ;) – VonC Nov 20 '12 at 08:22
  • @VonC: You could also rebase the newer part on the redone merge or you could redo the merge and manually apply diff between the bad and the fixed merge. Depends how much you can afford rewinding. – Jan Hudec Nov 20 '12 at 08:39
  • The conflicts might have been caused by the cherry-picks. Adding a section about dealing with cherry-picks so they don't cause conflicts in later full merges. – Jan Hudec Nov 20 '12 at 08:41
  • Ah! I see "`rebase`" in your (already quite complete) answer. +1 ;) – VonC Nov 20 '12 at 08:49
  • Wow, I'm not sure I understand this answer completely, but thank you for taking the time out to write such a detailed explanation. It is possible that during the merge of `master` INTO `topic`, commit `B` may have been reversed. If that is the case, shouldn't this show up in the changeset/log of the merge commit? Shouldn't I see the files `foo` and `bar` being changed? This is surprisingly not the case. Before the merge & after the merge `foo` and `bar` are different but the changeset for the merge has nothing about those files. – Saurabh Nanda Nov 20 '12 at 15:56
  • I recreated the merge and got conflicts. In the `staging` area I can see the `foo` and `bar` files. If I unstage `foo` and `bar` manually from the merge commit, this issue is recreated, i.e. commit B goes missing without a trace! So, I guess I found the culprit :-) Thank you for helping me out on this! – Saurabh Nanda Nov 20 '12 at 16:10
  • @SaurabhNanda: By default git diff only shows files that have changes from both parents for merge commits. Here the files only differed from one of them. It was the wrong one, but git diff does not do common ancestor lookup, so it can't tell the difference and won't show those files. – Jan Hudec Nov 21 '12 at 06:39
2

Not sure why B isn't there, but I would recommend rebasing topic on top of master instead of merging master into topic.

Your cherry-picked commits are duplicated into master, and that doesn't play well when merging master into a branch (topic) with those same commits already present.
This is different when you rebase topic on top of master: any duplicate commit is not replayed.

Of course, the issue with rebase is that is changes topic SHA1, which is problematic if you had already pushed topic to an upstream repo (and other people have already pulled topic).

In general, as illustrated in "What is the right git workflow with shared feature branches?", you would want to avoid "back-merge" (master to topic) and use mainly merges in one way only (topic to master).

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    _"In general, [...] you would want to avoid "back-merge" (master to topic) and use mainly merges in one way only"_. I don't think such "back-merges" are problematic (actually, they are a good idea to keep up-to-date with master, to make the final merge easier). The problem is if you mix cherry-picking and merging, isn't it? – sleske Nov 20 '12 at 07:55
  • @sleske "keep up-to-date with master" this could be done with a **rebase** of `topic` on top of `master`, not a merge. "The problem is if you mix cherry-picking and merging": yes: you apply duplicate commits twice which is why I don't like cherry-picking: see for instance http://stackoverflow.com/questions/13448560/use-git-fork-branches/13449086#13449086 as an alternative. – VonC Nov 20 '12 at 07:59
  • Yes, I think we agree: To keep a topic branch up-to-date, it's best to rebase it on top of master. If you want to share the topic branch, you will have to merge in master instead. Cherry-picking is problematic, because once you cherry-picked from a branch, merging it may no longer do the right thing. – sleske Nov 20 '12 at 08:03
  • 1
    @sleske: Merging works mostly fine after cherry-picking as long as the changes are not shadowed by further changes to the same place; if that happens, merge will produce conflict and you'll have to dig out which change obsoleted the other manually. – Jan Hudec Nov 20 '12 at 08:06
  • 1
    @sleske " Cherry-picking is problematic, because once you cherry-picked from a branch, merging it may no longer do the right thing", correct. Sometimes, cherry-picking is acceptable, because there is no "back-merge": see http://stackoverflow.com/questions/6594881/git-merge-only-the-changes-made-on-the-branch/13452947#13452947 – VonC Nov 20 '12 at 08:07
  • @VonC: I've been wondering if rebasing is a better idea in such cases. However, I'm not sure if I can rebase a local topic branch on top of a remote master if the local topic has been pushed to the central repo, but has NOT been pulled by other people. Any comments? – Saurabh Nanda Nov 20 '12 at 17:43
  • @SaurabhNanda you can rebase, except you will have to `git push -force` in order to publish the new history of your branch. – VonC Nov 20 '12 at 18:24
  • @VonC -- and in that case what happens to the previous commits in the remote repo? They'removed and new ones created? – Saurabh Nanda Nov 21 '12 at 04:43
  • @Saurabh Not removed immediately, but kept in the reflog. – VonC Nov 21 '12 at 05:42