问题
Morning all, Let's say I have a series of commits as follows:
- abc000
- def111
- abc222
- def333
- abc444
- def555
I know I can use
$ git diff -c --binary abc000..def555 > /tmp/abc000_def555.patch
to produce a patch to upgrade a system from abc000 to def555.
But what if I wanted to (for reasons to dull to get into) exclude def333 from the patch - how can I do this? Note that I do not want to revert def333, its just that I do not want the patch to incorporate this commit.
Thanks
回答1:
Actually all git diff
s are commit-pair-wise: the above compares the trees/files in abc000
against those in def555
. If for instance def333
changes dir/file
but there are no changes to dir/file
between abc000
and def555
(e.g., the change in def333
is canceled out by something along the way) you might not see dir/file
in there at all.
In general, though, changes made in def333
will have altered dir/file
in a way that shows up when comparing the version in abc000
against the one in def555
. So you will probably see that change.
The easiest way to get a diff that shows "what def555
would look like if def333
were reverted" is to do just that: create a tree (on a temporary branch) with the change reverted. To do it with an actual named branch, you might 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 don't want a temp branch? Well, that's trivial: just don't create one. Get a "detached HEAD" as above, do the revert as above, and then git checkout somebranch
. There's no temporary branch to delete, except for the unnamed one that git will warn that you are leaving behind ... which is just what you wanted.
回答2:
It is not possible to git diff
and exclude one or more commits. You have to make a new branch, revert the unwanted commits, then diff it and make a patch:
git checkout --branch <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.
p.s. I didn't want to butcher torek's answer too much so I posted a new one that I think is more concise.
回答3:
I would say you create a new branch, cherry-pick the commits you need and then do the diff
回答4:
You could try using git rebase -i and then drop the commit you don't need. Another way is to cherry-pick the commits you need on a different branch.
来源:https://stackoverflow.com/questions/18977073/exclude-a-single-commit-from-a-git-diff