Introduction
I recently got into a situation, where I had to edit multiple git commit messages. I was contributing to a project and had already added my commits. But, I had to change the commit messages to follow the project’s commit message guidelines.
Using git rebase to edit commit history
git rebase
is a powerful command that can be used to edit commit history. It can be used to edit commit messages, squash commits, reorder commits, and much more.
Before using git rebase
, make sure you:
- are in the correct branch
- have no uncommitted changes
If you are mid-way through some work and don’t wish to commit, you can use git stash
to save/stash your changes. Later you can use git stash pop
to get back your changes.
Command
git rebase -i HEAD~<number of commits to edit>
Here <number of commits to edit>
is the number of past commits you want to edit. For example, if you want to edit the last 5 commits, you can use git rebase -i HEAD~5
.
Output
pick 04afe81 create release 1.2.0 (#43)
pick 4438214 update instructions for adding favicon
pick 4e83639 chore(ci): add comments for GitHub action workflow trigger
pick 2514abd chore(ci): change emojis used in changelog
pick 1a2b3c4 docs: update README.md
# Rebase 4e83639..1a2b3c4 onto 4e83639 (5 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
Now, change the pick
to reword
for the commits you want to edit. In my case, I wanted to edit the last 2 commits. So, I changed the first 2 pick
to reword
.
Note: don’t edit the commit messages now. You’re just letting Git know which commits you’re interested in modifying.
reword 04afe81 create release 1.2.0 (#43)
reword 4438214 update instructions for adding favicon
pick 4e83639 chore(ci): add comments for GitHub action workflow trigger
pick 2514abd chore(ci): change emojis used in changelog
Save the file and close it. You will now be prompted to edit the commit messages for the commits you changed to reword
.
update instructions for adding favicon
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Mon May 15 18:01:56 2023 +0530
#
# interactive rebase in progress; onto 4e83639
# Last commands done (2 commands done):
# pick 04afe81 create release 1.2.0 (#43)
# pick 4438214 update instructions for adding favicon
# No commands remaining.
#
# Changes to be committed:
# modified: README.md
#
Edit the commit message and save the file. Repeat the same for the other commit.
Verify the changes
You can use git log --oneline
to verify the changes.
The output in my case was:
a12fe61 (HEAD -> master) chore: create release 1.2.0 (#43)
3c38245 docs: update instructions for adding favicon
4e83639 chore(ci): add comments for GitHub action workflow trigger
2514abd chore(ci): change emojis used in changelog
1a2b3c4 docs: update README.md
As you can see, the last 2 commit messages have been updated. Also, the commit hash has changed for the last 2 commits. This is because we edited the commit messages.
Conclusion
In this post, we learned how to edit multiple git commit messages using git rebase
. We also learned how to verify the changes. I hope you found this post useful.