I ran an interactive rebase up to commit abcdef
.
git rebase -i abcdef
In the editor - Vim I changed all pick hash
lines to
reword hash PREFIX: Original commit message
using this vim command
%s/pick \(\w\{7}\)/reword \1 PREFIX:/
but then git goes on to prompt me to edit the message for every commit. Is there a simple way to batch this process?
To change the most recent commit message, use the git commit --amend command. To change older or multiple commit messages, use git rebase -i HEAD~N .
Squash commits together. Two other commands rebase interactive offers us are: squash ( s for short), which melds the commit into the previous one (the one in the line before) fixup ( f for short), which acts like “squash”, but discards this commit's message.
Changing Multiple Commit Messages You can run rebase interactively by adding the -i option to git rebase . You must indicate how far back you want to rewrite commits by telling the command which commit to rebase onto. Remember again that this is a rebasing command — every commit in the range HEAD~3..
Just right-click on the commit you want to edit and select Interactively Rebase from Here… Select reword on the commit and click Start Rebasing. Edit the commit message and click Resume Rebasing.
GIT_EDITOR='sed -i "1s/^/PREFIX: /"' GIT_SEQUENCE_EDITOR=vim \
git rebase -i abcdef
or alternately you could
git -c core.editor='sed -i "1s/^/PREFIX: /"' \
-c sequence.editor=vim \
rebase -i abcdef
if you don't want to use the environment overrides.
If you know you're going to reword them all you could even sed the sequence, GIT_SEQUENCE_EDITOR='sed -i "s/^pick/reword/"'
.
Alternative answer using only git
:
git rebase -i --exec 'git commit --amend -m "PREFIX: $(git show -s --format=%s)"' origin/master
You can also do this non-interactively (without the -i
flag). Afterwards in the interactive window you can remove the exec directive after every commit where the prefix shouldn't be applied.
Explanation:
Using rebase --exec
you can execute a command for every commit, this is quite generally useful.
Using git commit --amend -m <MESSAGE>
you can change the message of the current commit.
Using git show -s --format=%s
you can display the current commit message.
Putting it all together:
To prefix the message we must get the current message (3), and change the current message with amend (2), i.e. git commit --amend -m "PREFIX: $(git show -s --format=%s)"
. The double quotes are important so that the command gets executed inside the message string.
To do this for every commit in the rebase (based on your origin/master
branch here) you execute (4) on every commit, using single quotes otherwise every commit will get your last commit's message, and voilà you get the command above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With