leftaroundabout
leftaroundabout

Reputation: 120711

Can git automatically split up a commit into single hunks?

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?

Upvotes: 4

Views: 392

Answers (1)

martin
martin

Reputation: 3239

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

Upvotes: 7

Related Questions