Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git prepend all commit messages in interactive rebase

Tags:

git

rebase

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?

like image 512
Peter Chaula Avatar asked Apr 11 '17 15:04

Peter Chaula


People also ask

How do I amend multiple commit messages?

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 .

How do you squash commits with interactive rebase?

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.

How do you complete interactive rebase?

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..

How do I edit a rebase interactive?

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.


2 Answers

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/"'.

like image 77
jthill Avatar answered Oct 22 '22 23:10

jthill


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:

  1. Using rebase --exec you can execute a command for every commit, this is quite generally useful.

  2. Using git commit --amend -m <MESSAGE> you can change the message of the current commit.

  3. Using git show -s --format=%s you can display the current commit message.

Putting it all together:

  1. 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.

  2. 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.

like image 23
CodeMonkey Avatar answered Oct 22 '22 23:10

CodeMonkey