Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

diff

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.

like image 676
Petteri H Avatar asked Mar 08 '12 09:03

Petteri H


4 Answers

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

like image 39
Oleksandr Kravchuk Avatar answered Oct 13 '22 11:10

Oleksandr Kravchuk


  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
like image 22
Lazy Badger Avatar answered Oct 13 '22 12:10

Lazy Badger


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.

like image 120
ralphtheninja Avatar answered Oct 13 '22 10:10

ralphtheninja


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.

like image 26
torek Avatar answered Oct 13 '22 12:10

torek