Exclude one commit from "git diff"

Morning is all, Let's say I have a number of commits:

  • abc000
  • def111
  • abc222
  • def333
  • abc444
  • def555

I know I can use

$ git diff -c --binary abc000..def555 > /tmp/abc000_def555.patch 

create a patch to update the system from abc000 to def555.

But what if I want (for reasons to stupidly fit in) to exclude def333 from the patch - how can I do this? Please note that I do not want to return def333, it is just that I do not want the patch to include this commit.

thanks

+8
git git-diff
source share
4 answers

In fact, all git diff are related to commit: the above compares trees / files in abc000 with tags in def555 . If, for example, def333 modifies dir/file , but there is no change in dir/file between abc000 and def555 (for example, a change in def333 canceled by something along the way), you may not see dir/file there at all.

In general, changes made to def333 will change dir/file in the way that appears when comparing a version in abc000 with one in def555 . So you are likely to see this change.

The easiest way to get diff, which shows "what def555 would look like if def333 was returned," should do just that: create a tree (on a temporary branch) with the modified change. To do this with the actual named branch, you can do something like this:

 git checkout def555 # by ID, so that you get a "detached HEAD" git checkout -b temp-branch git revert --no-edit def333 git diff [options] abc000 git checkout somebranch; git branch -D temp-branch 

What if you do not need the temp branch? Well, this is trivial: just don't create. Get a “separate HEAD” as above, return as above, and then git checkout somebranch . There is no temporary branch for deletion, other than an unnamed one, which git will warn you to leave ... this is exactly what you wanted.

+7
source share

It is not possible to git diff and exclude one or more commits. You should create a new branch, return unwanted commits, then split it and make a patch:

 git checkout -b <branch-name>-diff-branch // Create a throwaway branch. git revert <unwanted-sha> // Get rid of the unwanted commit. Do this as many times as necessary, you cannot revert multiple SHAs at once. git diff > new.patch // Create your new patch. git checkout - // Checkout previous branch, nice trick eh?! git branch --delete --force <branch-name>-diff-branch // Delete the throwaway branch. 

ps I didn’t want the butcher to answer too much, so I posted a new one, which I think is more concise.

+3
source share

I would say that you create a new branch, cherry pick the commits you need, and then do the diff

+1
source share

You can try using git rebase -i and then refuse a commit that you don't need. Another way is to cherry - select the commits you need in a different branch.

0
source share

All Articles