Skip to content

Change commit message

Henk Mutsaerts edited this page Dec 25, 2022 · 10 revisions

πŸ“ Change commit message

The solution described here can be used if you accidentally pushed a commit with the wrong commit message. This solution changes the message. However, note that this will change history - you have to inform all people working in the respective branch before carrying out the changes suggested below!

Run an iterative rebasing of the commits

Either specify the last N commits:

git rebase -S -i HEAD~N

or a commit range:

git rebase -S -i commitID^..HEAD

Modify the commit history

The interactive rebase command will automatically open an editor with a list of commits.

You need to change the word pick to reword for all those commits where you want to change the message (in vi: press i and type).

Save and quit the log (in vi: ESC an type :wq).

Modify each commit message

An editor will open automatically for each message to modify. Edit the commit message on the top. Note that it shouldn't start with # as this is here supposed to be a comment. Instead of "#123 Fix" write for example " #123 Fix" with an extra space at the beginning - this is the best way how to make this a commit message and not a comment that is ignored, but still do not have any extra text in the commit message.

Save and close the editor after each commit.

Push the changes

You can upload the changes to remote by git push -f

Note that this will change the history and commit IDs. So anybody working in this branch will have to reload it locally (ideally delete first) - simple pull will end up with having both the old and new commits and unnecessary duplicity.


Change the commit message of a merge

Can be useful after resolving a conflict, source

git checkout <sha of merge>
git commit --amend # edit message
git rebase HEAD previous_branch

(In the vim text editor: press i to insert, change the first line (commit message), press escape, type in :wq and press enter)

After the rebase, you will have to do a force push:

git push -f origin your_branch

ExploreASL ©️

Git Workflow Guide

Setting up Git with ExploreASL

Test Strategy

Advanced

Clone this wiki locally