Exclude a single commit from a “git diff”

丶灬走出姿态 提交于 2019-12-04 23:46:19

Actually all git diffs 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.

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.

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

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.

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