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.
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. |
||||
show 7 more comments |
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
but can be used to amend a merge commit. |
|||||||||||||||||||||
|
|
|||||||||||||||||||||
|
If the commit you want to fix isn’t the most recent one:
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 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? |
|||||||||||||||||||||
|
To amend the previous commit, make the changes you want and stage those changes, and then run
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
To fix the previous commit by removing it entirely, run
If you want to edit more than one commit message, run
(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
|
|||||||||||||||||
|
A plain
will run your editor and load the previous commit message. All you have to do is edit it and save. |
|||||||||
|
As already mentioned,
|
||||
|
You also can use git filter-branch for that.
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 ;-) |
|||||||||||||||||||||
|
I prefer this way.
Otherwise, there will be a new commit with a new commit ID |
|||||||||||||
|
|
||||
|
You can use Git rebasing. For example, if you want to modify back to commit bbc643cd, run
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
Now you can use
to modify the commit, and after that
to return back to the previous head commit. |
|||||
|
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 will create a temporary directory for rewriting and additionally backup old references in refs/original/.
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:
|
|||||
|
Use
To understand it in detail, an excellent post is 4. Rewriting Git History. It also talks about when not to use |
|||||||||||||
|
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
|
||||
|
If you are using the Git GUI, you can amend the last commit which hasn't been pushed with:
|
||||
|
You have a couple of options here. You can do |
||||
|
You can use Git rebasing. For example, if you want to modify back to commit
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
Now you can use
to modify the commit, and after that
to return back to the previous head commit. |
||||
|
I use git gui as much as I can, and that gives you the option to amend the last commit: Also, |
|||
|
Wow, so there are a lot of ways to do this. Yet another way to do this is to delete the last commit, but keep its changes so that you won't lose your work. You can then do another commit with the corrected message. This would look something like this:
I always do this if I forget to add a file or do a change. Remember to specify |
|||
|
If you just want to edit the latest commit use:
or
But if you want to edit several commits in a row you should use rebasing instead:
In a file like the one above write edit/e or one of the other option and hit save and exit. Now you'll be at the first wrong commit. Make changes in the files, and they'll be automatically staged for you. Type
save and exit that and type
to move to next selection until finished with all your selections. Note that these things change all your SHA hashes after that particular commit. |
||||
|
I have added the alias of
|
||||
|
Update your last wrong commit message with new commit message in one line:
OR, Try git reset like below:
git reset helps you to break one commit to multiple commits too
Here you have successfully broke your last commit into two commits. |
|||
|
To change a commit message of the most recent (unpushed) commit, you can simply use
To change messages of (unpushed) commits further in the past:
|
|||
|
You can also do in this way :
This will run your editor and load the previous commit message. You have to edit it and save. I hope this will surely help you! :) |
|||
|
This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.
HEAD
orhead
can now use@
in place ofHEAD
instead. See this answer (last section) to learn why you can do that. – Cupcake Jul 26 '13 at 2:04