git format-patch X..Y for a specific author

2020-07-09 11:08发布

问题:

I was wondering if you can generate patches for a range but only limit to commits from a specific author, the way you do with git log --author='bob'.

回答1:

Yes, it is possible.

According to reference on git format-patch it accepts <revision range>

Generic expression (see "SPECIFYING REVISIONS" section in gitrevisions(7)) means the commits in the specified range.

Details may be found in the reference but we only need this one:

^!, e.g. HEAD^!

A suffix ^ followed by an exclamation mark is the same as giving commit and then all its parents prefixed with ^ to exclude them (and their ancestors).

So you'll need:

git log X..Y --author='<AUTHOR>' --format="%H" | sed 's/$/^!/g' | xargs -I{} git format-patch {}

git log X..Y --author='<AUTHOR>' --format="%H" produces output in format of 40-digit sha1 sums.

sed 's/$/^!/g' adds ^! at the end of each line

xargs -I{} git format-patch {} just runs git format-patch with each line



标签: git patch