How to Change Older or Multiple Commit Messages
- Steps to changing older or multiple commits
- Git Rebase Standard and Git Rebase Interactive Modes
Probably, there are times when you need to edit a commit message. This tutorial explains the way 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, add files, and change the message. Let’s see how you can change the older or multiple commits by taking these easy steps below.
1. Finding the commit
Find the commit the message of which you want to change.
2. Running interactive rebase
Run git rebase -i HEAD~N. N is the number of commits on which you perform a rebase. For example:
pick 43f8707f9 fix: update dependency json5 to ^2.1.1 pick cea1fb88a fix: update dependency verdaccio to ^4.3.3
The git rebase command will display the latest x commits in the default text editor.
3. Replacing pick to reword
Move to the lines of the commit message you want to change and replace pick with reword. Reword or r stops the rebase process and gives a chance to amend the commit message. For example:
reword 43f8707f9 fix: update dependency json5 to ^2.1.1 reword cea1fb88a fix: update dependency verdaccio to ^4.3.3
4. Saving changes
After changing the messages save and close the editor. A new text editor opens for each chosen commit. All you need is to change the commit message, then save the file, and finally close the editor:
fix: update dependency json5 to ^2.1.1
5. Force pushing
Then, force push the changes to the remote repository running the following:
git push --force branch-name
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, as a tag, an ID, a branch name, and so on. The interactive mode is carried out with the -i 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.