Sathish
Sathish

Reputation: 2180

Understanding how git diff & git cherry-pick works

Here is the repro repo: https://github.com/sathishsoundharajan/git-diff

Steps

  1. Created new repository in github.
  2. Created master branch.
  3. Added master.txt file to master branch and committed.
  4. Created new branch named feature-branch from master.
  5. Added feature.txt file to feature branch and committed.
  6. Switch to master branch.
  7. Added a new file new-master.txt to master branch and committed ( CommitId: https://github.com/sathishsoundharajan/git-diff/commit/593da755a9ea90b6f55bcc6d184f249218bf4170)
  8. Now for some reason let's say i have to cherry-pick this commit to feature-branch.
  9. Switch to feature-branch.
  10. Use git cherry-pick 593da755a9ea90b6f55bcc6d184f249218bf4170. Cherry-picked without conflicts but the commit_id changed to https://github.com/sathishsoundharajan/git-diff/commit/e58427c82322d6a3ec933741887f5b8312981e11
  11. Now if we raise a pull request from feature-branch to master / take git diff between master and feature-branch, i should only see the feature.txt file alone in the diff. But i can see new-master.txt file also in the diff https://github.com/sathishsoundharajan/git-diff/pull/1

Questions:

  1. Why commit-id changed after cherry-pick ?
  2. Can we stop cherry-pick to not create new commit-id and reuse the existing one ?
  3. Why in git diff i'm seeing new-master.txt even though it is already in the master branch ?
  4. Is there way to make git does not show this diff ?
  5. Is there a way to see diff between two branches without comparing commit-ids and by comparing actual change in the files ?

Upvotes: 0

Views: 84

Answers (1)

eftshift0
eftshift0

Reputation: 30212

1 because history is different so the IDs can't be the same for security/cryptographic reasons.

2 no, related ro #1 ... if you got the same ID, that would mean you found a collision (or you are producing the exact same objects, like what git svn does)... theoretically possible but....

3 because in PR/MR, you see differences not between the tips of the two branches but since the feature branch is started, say (what you get if you try git diff with ... instead of ..). If you saw the differences between the 2 tlps of the branches, code review would be hell because you would see changes from the MR/PR itself plus the changes introduced in the target branch since the MR/PR branch was started (not to name that the changes will change over time as more stuff is merged unto the target branch). Who would want to work like that?

4 like the diff betwern the 2 branch tips? Try with .. instead of .... Now, on github? They might provide the option if they considered it necessay but I don't think you will find a lot of customers would be interested.... and then it's about them, not about git.

5 what do you mean?

Upvotes: 1

Related Questions