Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
752 views
in Technique[技术] by (71.8m points)

git diff with author filter

I have a series of commits by different authors and I would like to see a git dff output between 2 commits but only considering the commits by one of the authors, something like, something like --author in git log.

I am interested in the final summary diff and not the diffs of the individual commits.

Is there a git trick for that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem here is that you can't do this in the general case. Suppose Alice changes a particular file, then Bob changes it - including parts that Alice changed - and finally Alice changes it again. How do you combine Alice's two diffs into a single diff? If you take them as two patches, the second simply won't apply without Bob's patch being applied first! But you also can't simply diff the final state against the original, because that will include Bob's changes.

If you prefer an example with git operations, this is like doing an interactive rebase, and just deleting random commits. Sure, sometimes it'll work, but sometimes it'll just completely fail, because one of those commits depended on one of the ones you took out.

So, I know you said you don't want individual commit diffs, but that's all you can really hope for:

git log -p --author=Alice

Or if you're really desperate for a single diff, this will get it for you, but only in the cases where there's no patch interaction like I mentioned above:

git checkout -b temp first_commit
git log --reverse --pretty=%H --author=Alice first_commit..second_commit |
while read commit; do
    git cherry-pick $commit || exit
done
# or if you have a new version of git, cherry-pick works with multiple arguments:
# git cherry-pick $(git log --reverse --pretty=%H --author=Alice first_commit..second_commit)
git diff first_commit temp

This does really require operations in the work tree, because there's absolutely no guarantee that any of the patches will apply once a commit has been skipped. You just have to try and see.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...