git diff with author filter

24,176

Solution 1

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.

Solution 2

May be you could use the formatting features of diff-tree

format:<string>

The format:<string> format allows you to specify which information you want to show.
It works a little bit like printf format, with the notable exception that you get a newline with %n instead of \n.

E.g,

format:"The author of %h was %an, %ar%nThe title was >>%s<<%n" 

would show something like this:

The author of fe6e0ee was Junio C Hamano, 23 hours ago
The title was >>t4119: test autocomputing -p<n> for traditional diff input.<<

You can then grep on the relevant author.

Share:
24,176
Samer Buna
Author by

Samer Buna

Pluralsight Author | Organizer of ReactjsCamp

Updated on July 09, 2022

Comments

  • Samer Buna
    Samer Buna almost 2 years

    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?

  • brianary
    brianary about 5 years
    Excluding files that were never touched by the requested author doesn't seem like it should be intractable. If patches have to accumulate in the background to build the summary this question wants, just include all patches to a file after the first one by the requested author.