Answer of @Mureinik is good but not understandable by newbie.
First method:
- If you only want to edit latest commit message, then you only need
git commit --amend
, you would see:
<your existing commit mesage foo bar>
# Please enter the commit message fir your changes. Lines starting
# with # will be ignored, and an empty message aborts the commit.
#
# Date: Sat Aug 24 17:56:16 2019 +0800
#
# On branch is up to date with 'origin/master'.
#
# changes to be committed:
# modified: foo.py
#
- As you can see, commit message on top without any prefix of command such as
pick
, this is already the edit page and you can direct edit the top message and save&quit, e.g.:
<your new correction commit message>
# Please enter the commit message for your changes. Lines starting
....
- Then do
git push -u origin master --force
or <how you push normally> --force
. The key here is --force
.
Second method:
You can see the commit hash by git log
or extract from the
repository url, example in my case is 881129d771219cfa29e6f6c2205851a2994a8835
Then you can do git rebase --interactive 881129d771219cfa29e6f6c2205851a2994a8835
or git rebase -i HEAD^
(if the latest)
You would see:
pick <commit hash> <your current commit message>
# Rebase 8db7e8b..fa20af3 onto 8db7e8b
#
# 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
- But if you see
noop
then you are probably typing wrong, e.g. if you do git rebase -i 881129d771219cfa29e6f6c2205851a2994a88
which missing ^
at the end, you better quit the editor without save and figure out the reason:
noop
# Rebase 8db7e8b..fa20af3 onto 8db7e8b
...
- If no
noop
issue, then simply change the word pick
to reword
, other just remains (you don't edit commit message at this point), e.g:
reword <commit hash> <your current commit message>
# Rebase 8db7e8b..fa20af3 onto 8db7e8b
#
# Commands:
# p, pick = use commit
...
- Save&quit will see the edit page similar to method #1:
<your existing commit mesage foo bar>
# Please enter the commit message fir your changes. Lines starting
# with # will be ignored, and an empty message aborts the commit.
#
# Date: Sat Aug 24 17:56:16 2019 +0800
#
# interactive rebase in progress; onto b057371
# Last command done (1 command done):
# reword d996ffb <existing commit message foo bar>
# No commands remaining.
# You are currently editing a commit while rebasing branch 'master' on 'b057371'.
#
# changes to be committed:
# modified: foo.py
#
- Edit the message on top, same like method #1 and save&quit, e.g:
<your new correction commit message>
# Please enter the commit message for your changes. Lines starting
....
- Again, same like method #1, do
git push -u origin master --force
or <how you push normally> --force
. The key here is --force
.
For more info please read the doc.
You can try reverting the commit (see some options in this SO question: https://stackoverflow.com/questions/4114095/revert-to-previous-git-commit) - only make sure you first back up any code changes so you won't lose those for the sake of a comment!
– Traveling Tech Guy – 2014-05-09T17:25:24.5031
See also How do I edit an incorrect commit message in Git? on Stack Overflow.
– Arjan – 2014-05-10T10:38:38.577