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'
.
2 Answers
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
-
2This works great, the only mild issue is that it breaks the increment number in the patch naming (ie they're all prefixed with
0001
for some reason), but that's not a big deal in my case. Thanks!– tirdadcMar 6, 2015 at 12:39 -
6This modified command will help provide an incremented number:
git log --author='<AUTHOR>' --format="%H" --reverse | sed 's/$/^!/g' | nl -w 1 -s ' ' | xargs -I{} sh -c "git format-patch --start-number {}"
– jdknightJun 9, 2017 at 22:16
Note that the modified solution proposed by jdknight in the comments involves --reverse
.
git log --author='<AUTHOR>' --format="%H" --reverse | \
sed 's/$/^!/g' | nl -w 1 -s ' ' | \
xargs -I{} sh -c "git format-patch --start-number {}"
That is an order issue, highlighted with With Git 2.39 (Q4 2022), "git diff
"(man) rev^!" did not show combined diff to go to the rev from its parents.
See commit a79c6b6, commit 9f91da7, commit 793c211 (01 Oct 2022) by René Scharfe (rscharfe
).
(Merged by Junio C Hamano -- gitster
-- in commit b988427, 25 Oct 2022)
revisions.txt
: unspecify order of resolved parts of^!
Signed-off-by: René Scharfe
gitrevisions(7) says that
<rev>^!
resolves to<rev>
and then all the parents of<rev>
.
revision.c::handle_revision_arg_1()
actually adds all parents first, then<rev>
.
Change the documentation to leave the order unspecified, to avoid misleading readers.
revisions
now includes in its man page:
<rev>^!
, e.g.HEAD^!
:A suffix '
^
' followed by an exclamation mark is the same as giving commit '<rev>
' and all its parents prefixed with '^
' to exclude them (and their ancestors).