There are two ways to edit a Git commit message:
1. Edit the Latest Commit Message
To edit the latest commit message, we can create a new commit using the --amend
option with a new message.
For example, here is my Git log:
git log --oneline
e3a1c7d feat: efid suer profile
75c9f8c feat: add user profile page
9a3b9f3 chore: initial commit
Next, edit the latest commit message using the --amend
option:
git commit --amend -m "feat: edit user profile"
Check the Git log:
git log --oneline
537f21c feat: edit user profile
75c9f8c feat: add user profile page
9a3b9f3 chore: initial commit
As we can see from the output, the latest commit message has been edited.
2. Edit Older Commit Messages
To edit older commit messages, we can use the git rebase -i HEAD~3
command. The number 3
represents how many recent commits you want to edit, and it can be replaced with any other number. For example:
git rebase -i HEAD~3
After the command is executed, a new editor will appear showing the older commit messages. Change pick
to reword
for each commit whose message you want to edit. Then, save and close the editor by pressing Ctrl + X
.
pick 537f21c feat: edit user profile
reword 75c9f8c feat: add user profile page
reword 9a3b9f3 chore: initial commit
# Rebase e3a1c7d..9a3b9f3 onto 9a3b9f3 (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop to amend the commit
# 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
# b, break = stop here (continue rebase later)
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
After all commits have been edited, check the Git log:
git log --oneline
537f21c feat: edit user profile
75c9f8c feat: detail user profile page
9a3b9f3 setup project
As we can see from the output, the older commit messages have been edited.
Push the Changes to the Git Server
After the changes are made locally, we need to push them to the Git server (GitHub, GitLab, etc.) to sync the commit history. To do this, we need to add the -f
option to the git push
command. For example:
git push -f origin branch
Top comments (0)