How do I edit an incorrect commit message in Git?
71
I stupidly did a Git commit while half asleep, and wrote the totally wrong thing in the commit message. How do I change the commit message? 

I have not yet pushed the commit to anyone.
improve this question | comment
Quentin Jerde Created at: 2013-11-13 17:07:45 UTC By Quentin Jerde
For those somewhat new to git: Laurie's point about having not yet pushed is important.  Like rebasing, this is changing the history.  If someone has cloned/pulled from your repo between the original and rewritten history then they won't be able to pull after the rewrite (for that branch). - Brown Zboncak
So, how could one change the commit message after one have pushed? - Gabriella Gutkowski
@MathiasMadsenStav You would just need to git push --force. - Vidal Berge
But that would suck. Never ever amend/rebase after push. This basically breaks the whole merge stuff when another developer wants to pull your changes. - Maya Gaylord
@skot Things are likely to break and screw up the history in that case though. If you have to rebase commits which are parent commits in other branches/instances, a pull from these will re-import those commits after the rebase, which will kinda screw up the history. - Hulda Zboncak
19 Answers
0
If you are at the tip of the current branch, a simple 

$ git commit --amend 


will lead you to editor screen wherein you can modify the commit message.

If you want to do modification to your files along with commit message:

// reset the head
$ git reset --soft HEAD^


Then you can make you changes, and commit again with:

$ git commit -c ORIG_HEAD

0
To amend the previous commit, make the changes you want and stage those changes, and then run



git commit --amend


This will open a file in your text editor representing your new commit message. It starts out populated with the text from your old commit message. Change the commit message as you want, then save the file and quit your editor to finish.

To amend the previous commit and keep the same log message, run

git commit --amend -C HEAD


To fix the previous commit by removing it entirely, run

git reset --hard HEAD^


If you want to edit more than one commit message, run

git rebase -i HEAD~commit_count

(Replace commit_count with number of commits that you want to edit.) This command launches your editor. Mark the first commit (the one that you want to change) as “edit” instead of “pick”, then save and exit your editor. Make the change you want to commit and then run

git commit --amend
git rebase --continue

0
If you are using the Git GUI, you can amend the last commit which hasn't been pushed with:  

Commit/Amend Last Commit

0
A simple 

git commit --amend


command will run an editor with all previous messages. You simply have to edit them...
0
I use git gui as much as I can, and that gives you the option to amend the last commit:



Also, git rebase -i origin/masteris a nice mantra that will always present you with the commits you have done on top of master, and give you the option to amend, delete, reorder or squash. No need to get hold of that hash first.
0
You have a couple of options here. You can do git commit --amend as long as it's your last commit. Otherwise if it's not your last commit you can do an interactive rebase. git rebase -i [branched_from] [hash before commit]. Then inside the interactive rebase you simply add edit to that commit. When it comes up do a git commit --amend and modify the commit message. If you want to roll back before that commit point you could also use git reflog and just delete that commit. Then you just do a git commit again.
0
git commit --amend -m "New commit message"


Used to amend the tip of the current branch. Prepare the tree object you would want to replace the latest commit as usual (this includes the usual -i/-o and explicit paths), and the commit log editor is seeded with the commit message from the tip of the current branch. The commit you create replaces the current tip -- if it was a merge, it will have the parents of the current tip as parents -- so the current top commit is discarded.

It is a rough equivalent for:

$ git reset --soft HEAD^
$ ... do something else to come up with the right tree ...
$ git commit -c ORIG_HEAD


but can be used to amend a merge commit.
0
git commit --amend -m "your new message"

0
If the commit you want to fix isn’t the most recent one:

git rebase --interactive $parent_of_flawed_commit

If you want to fix several flawed commits, pass the parent of the oldest one of them.
An editor will come up, with a list of all commits since the one you gave.

Change pick to reword (or on old versions of Git, to edit) in front of any commits you want to fix.
Once you save, Git will replay the listed commits. 

Git will drop back you into your editor for every commit you said you want to reword and into the shell for every commit you wanted to edit. If you’re in the shell:

Change the commit in any way you like.
git commit --amend
git rebase --continue

Most of this sequence will be explained to you by the output of the various commands as you go. It’s very easy, you don’t need to memorise it – just remember that git rebase --interactive lets you correct commits no matter how long ago they were.

Note that you will not want to change commits that you have already pushed. Or maybe you do, but in that case you will have to take great care to communicate with everyone who may have pulled your commits and done work on top of them. How do I recover/resynchronise after someone pushes a rebase or a reset to a published branch?
0
Use

git commit --amend


To understand it in detail, an excellent post is 4. Rewriting Git History. It also talks about when not to use git commit --amend.
0
A plain

git commit --amend


will run your editor and load the previous commit message. All you have to do is edit it and save.
0
As already mentioned, git commit --amend is the way to overwrite the last commit. One note: if you would like to also overwrite the files, the command would be 

git commit -a --amend -m "My new commit message"
0
I prefer this way. 

git commit --amend -c <commit ID>


Otherwise, there will be a new commit with a new commit ID
0
You also can use git filter-branch for that.

git filter-branch -f --msg-filter "sed 's/errror/error/'" $flawed_commit..HEAD

It's not as easy as a trivial "git commit --amend", but it's especially useful, if you already have some merges after your erroneous commit message.

Note that this will try to rewrite EVERY commit between HEAD and the flawed commit, so you should choose your msg-filter command very wise ;-)
0
If you only want to modify your last commit message, then do:

$ git commit --amend
That will drop you into your text exitor and let you change the last commit message.
If you want to change the last 3 commit messages, or any of the commit messages up to that point, supply 'HEAD~3' to the git rebase -i command.

$ git rebase -i HEAD~3
0
You can use Git rebasing. For example, if you want to modify back to commit bbc643cd, run

$ git rebase bbc643cd^ --interactive


In the default editor, modify 'pick' to 'edit' in the line whose commit you want to modify. Make your changes and then stage them with

$ git add <filepattern>


Now you can use

$ git commit --amend


to modify the commit, and after that

$ git rebase --continue


to return back to the previous head commit.
0
If you have to change an old commit message over multiple branches (i.e., the  commit with the erroneous message is present in multiple branches) you might want to use 

git filter-branch -f --msg-filter 'sed "s/<old message>/<new message>/g"' -- --all


Git will create a temporary directory for rewriting and additionally backup old references in refs/original/.

-f will enforce the execution of the operation. This is necessary if the the temporary directory is already present or if there are already references stored under refs/original. If that is not the case, you can drop this flag.

-- separates filter-branch options from revision options

--all will make sure, that all branches and tags are rewritten.

Due to the backup of your old references, you can easily go back to the state before executing the command.

Say, you want to recover your master and access it in branch old_master:

git checkout -b old_master refs/original/refs/heads/master

0
You can use Git rebasing. For example, if you want to modify back to commit bbc643cd, run

$ git rebase bbc643cd^ --interactive

In the default editor, modify 'pick' to 'edit' in the line whose commit you want to modify. Make your changes and then stage them with

$ git add <filepattern>

Now you can use

$ git commit --amend

to modify the commit, and after that

$ git rebase --continue

to return back to the previous head commit.
0
if you are using the Git GUI tool there is a button named amend last commit. Click on that button and then it will display your last commit files and message. Just edit that message and you can commit it with new commit message.

Or use this command from a console/ terminal:

git commit -a --amend -m "My new commit message"

Your Answer