简体   繁体   中英

Why does a cherry-pick cause git merge to discard part of my commit?

So I'm trying to understand why when I git merge master into a branch, I lose some, but not all of my changes in a commit. It seems to do with a cherry-pick commit on my branch. Here's how to reproduce:

  1. init a repo with an initial commit
  2. Add a line of text to a file and commit
  3. Branch off this, call it "test"
  4. On branch master, revert the second commit
  5. Cherry-pick that commit into the test branch
  6. Go back to master, create a new commit that restores that line of text, plus add additional changes
  7. Go back to test, run git merge master
  8. Notice that the merge automatically removed that line of text (,.,), but kept the additional changes. I expect the merge to not drop that line, so that it matches master .

I uploaded my results on github: https://github.com/terryttsai/testRevertMerge/commits/test

If I never cherry-picked that commit into my test branch, then git merge master does keep that line of text. So how come when I do have that cherry-pick commit, the merge removes it? How can I prevent this from happening in the first place?

I expect the merge to not drop that line, so that it matches master.

No. git merge master does not mean to match master!

What is a merge? A merge means to play out the contributions of branches, starting from the point of divergence.分支的贡献。 What are they?

Well, the point of divergence is after that first line was added. So what happened after that on each branch?

  • On master, the only contribution is the extra stuff. (It removed the first line but later restored it, so that is not a contribution; in that respect nothing happened at all.)

  • On test, the only contribution is deletion of the first line (that is what the cherry picked revert commit does).

So the merge does both of them: the extra lines from master, and the deletion from test.

But if you never do the cherry pick, then neither branch makes a contribution that deletes that line, so obviously it is not deleted by the merge.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM