If you need to change the commit message for all commits, you can use an interactive rebase. Here’s how:
Start an interactive rebase with the parent commit of the first commit you want to change. If you want to change all commits, you can use git rebase -i –root.
git rebase -i –root
In the text editor that opens, you’ll see a list of all commits in reverse chronological order (the most recent commit at the top). Each line starts with the word pick.
Change pick to reword for each commit you want to change the message of.
Save and close the editor.
For each commit you’ve marked for reword, git will open your editor. Change the commit message, save the file, and close the editor.
Once you’ve changed all commit messages, git will reapply the commits.
If you’ve already pushed your commits to a remote repository, you’ll need to force push with git push origin <branch-name> –force-with-lease.
Please note that this changes the commit history, which can be problematic if others are working on the same branch. Always coordinate with your team when performing operations like this.
If you have opened the file in a terminal-based text editor like Vim, Nano, or Emacs, you can save the file with the following commands:
In Vim:
Press Esc to ensure you’re in command mode.
Type :wq and press Enter. This writes the changes to the file (w) and then quits (q).
In Nano:
Press Ctrl+O to write the changes.
Press Enter to confirm the filename.
Press Ctrl+X to exit Nano.
In Emacs:
Press Ctrl+x Ctrl+s to save the changes.
Press Ctrl+x Ctrl+c to exit Emacs.