Git diff tool over several commits with other's commit inbetween

人走茶凉 提交于 2019-12-10 15:29:47

问题


We have a workflow where committed code needs to be reviewed by other devs. In simple cases this can be done with "git diff oldhash newhash > diff.txt" and upload that to our review board.

But is there a way to create a diff over several commits and exclude commits done in between by someone else. For example I want to create diff over mine1 to mine4 but exclude Joe's commit :

mine4
mine3
joe's
mine2
mine1

Any ideas how to do this in command line git or with some other tool?

The commits made by others affect different files than my commits, so in this case it is just about excluding changes made by others.


回答1:


You could achieve this by creating a new branch combined with cherry-pick:

git checkout -b mine_diffs
git cherry-pick mine1
git cherry-pick mine2
git cherry-pick mine3
git cherry-pick mine4
git diff mine1_ mine4_

When you're done, just delete the branch. Note that the sha1 hashes will be different when diffing, which is what mine1_ and mine4_ indicates.




回答2:


I'm not quite sure what you mean by "over mine1 to mine4". Diffs are always pairwise (except for mid-merge); "git diff mine1 mine4" will give you all the changes between those two trees. Given the above example, you can get four separate patches: mine1->mine2, mine2->joes, joes->mine3, mine3->mine4. If you did mine1->mine2, mine2->mine3, mine3->mine4 you'd still "see" joe's changes.

Perhaps what you want is (as @OleksandrKravchuk suggested) "a diff of what one would have, if one cherry-picked the changes in mine2, mine3, and mine4 into a branch that started from mine1". In that case, you'll have to do just that: create such a branch, pick those changes to apply, and then generate the diff.

You could automate this fairly easily by creating the temporary branch and doing the sequence of "git cherry-pick"s, skipping the commit(s) you want to omit.




回答3:


Okay, maybe it is not true git way, but I would create new branch, removed unneeded commits and compared two branches.




回答4:


  1. Reviewing a bundle of commits as one diff is bad style: Codereview (if used) must work on single-commit level
  2. You can't have "sparse" diffs
  3. Maybe The Right Tools will be The Right Way? For Git it's Gerrit


来源:https://stackoverflow.com/questions/9615200/git-diff-tool-over-several-commits-with-others-commit-inbetween

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!