How to Change Older or Multiple Git Commit Messages
Sometimes, you need to go back to the older commits and modify commit messages. In this tutorial find how to change the older and multiple commit messages.
Probably, there are times when you need to edit a commit message. This tutorial explains how you can change the message of older or multiple git commits.
Steps to changing older or multiple commits
The interactive rebase allows you to stop on each commit to change the message, add files, or squash commits. Let’s see how you can change older or multiple commit messages by following these steps.
Finding the commit
Identify the commit whose message you want to change.
Running interactive rebase
Run git rebase <kbd class="highlighted">-i HEAD~N</kbd>. N is the number of commits you want to rebase. For example:
pick 43f8707f9 fix: update dependency json5 to ^2.1.1
pick cea1fb88a fix: update dependency verdaccio to ^4.3.3
Git will display the latest N commits in your default text editor.
Replacing pick with reword
Move to the lines for the commits you want to change and replace pick with reword. <kbd class="highlighted">Reword</kbd> (or <kbd class="highlighted">r</kbd>) stops the rebase process and allows you to amend the commit message. Note: Use reword only to change commit messages. To add or remove files, use edit (or e) instead. For example:
reword 43f8707f9 fix: update dependency json5 to ^2.1.1
reword cea1fb88a fix: update dependency verdaccio to ^4.3.3
Saving changes
After updating the messages, save and close the editor. A new text editor will open for each selected commit. Update the message, save the file, and close the editor:
fix: update dependency json5 to ^2.1.1
Force pushing
Then, force push the changes to the remote repository:
git push --force <branchname>Warning: Force pushing rewrites shared branch history. Only use this on branches that are not shared with others, or coordinate with your team to avoid conflicts.
Tip: If the rebase encounters conflicts or errors, run
git rebase --abortto cancel the operation and return to the original state.
Git Rebase Standard and Git Rebase Interactive Modes
The git rebase command has two modes: standard and interactive. The standard mode automatically applies the commits in the current working branch to the passed branch head. The current branch will be rebased onto <base>. This can be different kinds of commit references, such as a tag, an ID, a branch name, and so on. The interactive mode is carried out with the <kbd class="highlighted">-i</kbd> flag, which stands for “interactive”. The advantage of the interactive mode is altering the individual commits in the process, without moving all the commits to the new base. This mode cleans the history by removing and amending the existing sequence of commits.