Interactive Rebase (For Older or Multiple Commits)
If you need to update messages for multiple commits or older commits, use interactive rebase:
- Start an interactive rebase:
git rebase -i HEAD~n
Note: Replace `n` with the number of commits you want to go back
- In the interactive editor, choose the commit(s) you want to edit by replacing `pick` with `reword` (or `edit` for more complex changes).
- Update the commit messages as necessary and save the changes.
- Complete the rebase process by following the prompts. If you used `edit`, you will need to re-commit the changes.
Important: Be cautious when rewriting shared commit history, especially if other collaborators are working on the same branch.
How to Change Commit Message in Git?
Changing a commit message in Git can be done in a few different ways, depending on whether the commit is the most recent one or an earlier commit. Here’s an article on how to change a commit message in Git, covering scenarios both before and after the commit has been pushed to a remote repository.
These are the following topics that we are going to discuss:
Table of Content
- Changing the Most Recent Commit Message (Not Pushed Yet)
- Changing a Commit Message That Has Already Been Pushed
- Interactive Rebase (For Older or Multiple Commits)
- Practical Example