Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

how to `git diff` after `cherry pick`?

a - b - c - d   Master
     
       e - f - g Feature

After cherry-pick,I cannot diff commit f.

git checkout master  
git cherry-pick f
git diff f 

how to git diff after cherry pick?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It depends what you mean by "f".

Perhaps it would help to have a clearer mental picture of what a cherry-pick does? It copies a commit, giving the copy a different hash. So there are now two commits with the same message, but with different hashes.

It also depends on your understanding of git diff. It may be that the phrase git diff <some-commit> needs some clarification; perhaps what you think it does is not what it really does.


First, you cannot really be saying git diff f, because f is not the name of a commit. So what are you really saying? Which hash are you specifying? Are you saying

git diff <the-original-f>

or are you saying

git diff <new-copy-of-f>

??

If you git diff <the-original-f>, you should see something in response.

But if you git diff <new-copy-of-f>, meaning the copy of f that is now on the end of master, naturally git diff will be empty, because you are comparing two identical things.

Why? Well, that brings me to my second point. The simple command git diff <commit> compares that commit to the working tree. Well, when you cherry pick, the working tree is updated to match the commit that you just cherry picked. So there is indeed no difference.


To demonstrate, I constructed your graph in real life. Here's the log:

* e09ed05 (HEAD -> feature) g
* 534df94 f
* d8b09e2 e
| * a01de45 (master) d
| * 9c56924 c
|/  
* 7f2fdd8 b
* e7a240b a

As you can see, feature branches off from master at b, just as in your first graph. Okay, now I will do what you did: I checkout master and cherry-pick 534df94 (that is my "f"). And we get this:

* d9cc51a (HEAD -> master) f
* a01de45 d
* 9c56924 c
| * e09ed05 (feature) g
| * 534df94 f
| * d8b09e2 e
|/  
* 7f2fdd8 b
* e7a240b a

So now, as you see, there are two "f" commits: the original, 534df94 on feature, and the new copy, d9cc51a on the end of master.

Well, if I now git diff 534df94 (the old "f"), there is a difference. But if I git diff d9cc51a (the new "f"), there is no difference and no response. And that is correct and expected. My working tree is identical to commit d9cc51a. This is exactly like saying git diff master, and I am on master; master is HEAD (as you can see from the log).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...