As explained in previous questions, you can split up commits into smaller patches. However, these answers recommend git add -p
, which does the job but is tedious if all I want is to have one commit per hunk, in the given order in the file. Is there some way to achieve just that, automatically?
Asked
Active
Viewed 308 times
4

Community
- 1
- 1

leftaroundabout
- 117,950
- 5
- 174
- 319
-
1In case you're wondering why I need this: I have a huge commit, due to a change by an automatic code-formatting tool. This caused a runtime bug; if each hunk were in its own commit I could easily use `git bisect` to find it. – leftaroundabout May 21 '15 at 11:50
-
What would be a rule to delimit hunks? One file = one commit? – Nick Volynkin May 21 '15 at 11:55
-
@NickVolynkin: I'd be ok with the hunks that `git add -p` offers. – leftaroundabout May 21 '15 at 12:00
1 Answers
7
You could do something like
echo -e "y\nq" | git add -p && git commit -m "automated"
which does:
echo y (accept first hunk), then q (quit) for the next hunk, commit with the given message.
Loop until git commit
does not return success:
s=0
i=1
while [ $s -eq 0 ]
do
echo -e "y\nq" | git add -p && git commit -m "automated $i"
s=$?
let i=$i+1
done
or in one line:
s=0; i=0; while [ $s -eq 0 ]; do echo -e "y\nq" | git add -p && git commit -m "automated $i"; s=$?; let i=$i+1; done
Produces commits like
c5ba3 - (HEAD) automated 3
14da0 - automated 2
6497b - automated 1

martin
- 3,149
- 1
- 24
- 35
-
More a `bash` / `sh` solution than `git`, seems rather hackish, but I'd be ok with that if no native alternative is available. – leftaroundabout May 21 '15 at 12:01
-
1
-
@leftaroundabout yes, I thought so, but it worked nicely for my small test ;). – martin May 21 '15 at 12:04
-