简体   繁体   English

了解 git diff 和 git cherry-pick 的工作原理

[英]Understanding how git diff & git cherry-pick works

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

Steps脚步

  1. Created new repository in github.在 github 中创建了新的存储库。
  2. Created master branch.创建了主分支。
  3. Added master.txt file to master branch and committed.将 master.txt 文件添加到 master 分支并提交。
  4. Created new branch named feature-branch from master.从 master 创建了一个名为 feature-branch 的新分支。
  5. Added feature.txt file to feature branch and committed.将 feature.txt 文件添加到功能分支并提交。
  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 )添加一个新文件 new-master.txt 到 master 分支并提交( 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 .使用git cherry-pick 593da755a9ea90b6f55bcc6d184f249218bf4170 Cherry-picked without conflicts but the commit_id changed to https://github.com/sathishsoundharajan/git-diff/commit/e58427c82322d6a3ec933741887f5b8312981e11樱桃采摘没有冲突,但 commit_id 更改为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.现在,如果我们从 feature-branch 向 master 提出拉取请求/在 master 和 feature-branch 之间使用 git diff,我应该只在 diff 中看到 feature.txt 文件。 But i can see new-master.txt file also in the diff https://github.com/sathishsoundharajan/git-diff/pull/1但我也可以在差异https://github.com/sathishsoundharajan/git-diff/pull/1 中看到 new-master.txt 文件

Questions:问题:

  1. Why commit-id changed after cherry-pick ?为什么在cherry-pick 之后commit-id 改变了?
  2. Can we stop cherry-pick to not create new commit-id and reuse the existing one ?我们可以停止选择不创建新的提交 ID 并重用现有的吗?
  3. Why in git diff i'm seeing new-master.txt even though it is already in the master branch ?为什么在 git diff 中我看到 new-master.txt 即使它已经在 master 分支中?
  4. Is there way to make git does not show this diff ?有没有办法让 git 不显示这个差异?
  5. Is there a way to see diff between two branches without comparing commit-ids and by comparing actual change in the files ?有没有办法在不比较提交 ID 和比较文件中的实际更改的情况下查看两个分支之间的差异?

1 because history is different so the IDs can't be the same for security/cryptographic reasons. 1 因为历史不同,因此出于安全/加密原因,ID 不能相同。

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.... 2 不,相关的 ro #1 ...如果你有相同的 ID,那意味着你发现了碰撞(或者你正在产生完全相同的对象,就像git svn所做的那样)......理论上是可能的,但是......

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 .. ). 3 因为在 PR/MR 中,您看到的不是两个分支的提示之间的差异,而是因为功能分支已启动,所以说(如果您尝试使用...而不是.. git diff会得到什么)。 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).如果您看到分支的 2 个 tlps 之间的差异,代码审查将是地狱,因为您会看到 MR/PR 本身的更改以及自 MR/PR 分支启动以来在目标分支中引入的更改(不要命名那个随着更多内容合并到目标分支,更改将随着时间的推移而发生变化)。 Who would want to work like that?谁愿意这样工作?

4 like the diff betwern the 2 branch tips? 4 喜欢 2 个分支提示之间的差异吗? Try with .. instead of ... .尝试使用..而不是... Now, on github?现在,在 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.如果他们认为这necessay,但我不认为你会发现很多客户有兴趣....然后它是关于他们,不是混帐他们可能提供的选项。

5 what do you mean? 5 你是什么意思?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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