git diff with author filter

前端 未结 2 1469
伪装坚强ぢ
伪装坚强ぢ 2020-12-06 09:00

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 author

相关标签:
2条回答
  • 2020-12-06 09:38

    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.

    0 讨论(0)
  • 2020-12-06 09:47

    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.

    0 讨论(0)
提交回复
热议问题