I wrote the wrong thing in a commit message.
How can I change the message? The commit has not been pushed yet.
|
I wrote the wrong thing in a commit message. How can I change the message? The commit has not been pushed yet. |
||||
Amending the commit message
Will open your editor, allowing you to change the commit message of the most recent commit. Additionally, you can set the commit message directly in the command line with:
…however, this can make multi-line commit messages or small corrections more cumbersome to enter. Make sure you don't have any working copy changes before doing this or they can get committed too. Changing the message of a commit that you've already pushed to your remote branchIf you've already pushed your commit up to your remote branch, then you'll need to force push the commit with
Warning: force-pushing will overwrite the remote branch with the state of your local one. If there are commits on the remote branch that you don't have in your local branch, you will lose those commits. Warning: be cautious about amending commits that you have already shared with other people. Amending commits essentially rewrites them to have different SHA IDs, which poses a problem if other people have copies of the old commit that you've rewritten. Anyone who has a copy of the old commit will need to re-synchronize their work with your newly re-written commit, which can sometimes be difficult, so make sure you coordinate with others when attempting to rewrite shared commit history, or just avoid rewriting shared commits altogether. Documentation |
|||||||||||||||||||||
|
|
If the problem was extra files you commited (and you don't want those on repository), you can remove them using git rm and then commiting with --amend
You can also remove entire directories with -r, or even combine with other Bash commands
After removing the files, you can commit, with --amend option
This will rewrite your recent local commit removing the extra files, so, these files will never be sent on push and also will be removed from your local .git repository by GC. |
|||
|
|
|
For editing more than just the latest message (for commits that haven't been pushed), I'd recommend SourceTree (a Windows/Mac GUI):
Procedure:
|
||||
|
|
|
If it's your last commit, just amend the commit:
(using the
Find the commit you want, change Miniature vim tutorial (or, how to rebase with only 8 keystrokes
If you edit text a lot, then switch to the Dvorak keyboard layout, learn to touch-type, and learn vim. Is it worth the effort? Yes. ProTip™: Don't be afraid to experiment with "dangerous" commands that rewrite history* — Git doesn't delete your commits for 90 days by default; you can find them in the reflog:
* Watch out for options like |
||||
|
|
|
If you only want to change your last message you should use the
This ensures that you don't accidentally enhance your commit with staged stuff. Of course it's best to have a proper |
|||||||||||||||||||||
|
|
You can use git-rebase-rework It is designed to edit any commit (not just last) same way as
|
|||||
|
|
If you want to change an old commit message, you can checkout a new branch (tmp) for that commit, amend a new message, and then checkout back and rebase tmp.
Done! |
||||
|
|
|
If you have not pushed the code to your remote branch (GitHub/Bitbucket) you can change the commit message on the command line as below.
If you're working on a specific branch do this:
If you've already pushed the code with the wrong message, and you need to be careful when changing the message. That is, after you change the commit message and try pushing it again, you end up with having issues. To make it smooth, follow these steps. Please read my entire answer before doing it.
Important note: When you use the force push directly you might end up with code issues that other developers are working on the same branch. So to avoid those conflicts, you need to pull the code from your branch before making the force push:
This is the best practice when changing the commit message, if it was already pushed. |
||||
|
|
|
I like to use the following:
|
||||
|
|
|
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
Note: You can "Make the change you want" also from the editor opened by |
|||||||||||||
|
|
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:
|
||||
|
|
|
On this question there are a lot of answers but none of them explains in super detail how to change older commit messages using VIM. I was stuck trying to do this myself, so here I'll write down in detail how I did this especially for people who have no experience in VIM! I wanted to change my five latest commits that I already pushed to the server. This is quite 'dangerous' cause if someone else already pulled from this you can mess things up by changing the commit messages. However when you’re working on your own little branch and are sure no one pulled it you can change it like this: Let's say you want to change your five latest commits, then you type this in the terminal:
This command will get you into VIM there you can ‘edit’ your commit history. You’ll see your last 5 commits at the top like this:
Instead of Then you need to save and quit this screen, you do that by first going in to ‘command-mode’ by pressing the esc button. (you can check that you’re in command-mode if the word INSERT at the bottom has disappeared) Then you can type in a command by typing Then VIM wil go over every commit message you want to reword, here you can actually change the commit messages. You’ll do this by going into INSERT-mode, changing the commit message, going into the command-mode and save and quit. Do this 5 times and you’re out of VIM! Then, if you already pushed your wrong commits, you need to Now you have changed your commit messages! (As you see I'm not that experienced in VIM so if I used wrong 'lingo' to explain what's happening, feel free to correct me!) |
|||||||||
|
|
I realised that I had pushed a commit with a typo in it. In order to undo, I did the following:
Warning: force pushing your changes will overwrite the remote branch with your local one. Make sure that you aren't going to be overwriting anything that you want to keep. Also be cautious about force pushing an amended (rewritten) commit if anyone else shares the branch with you, because they'll need to rewrite their own history if they have the old copy of the commit that you've just rewritten. |
|||||
|
|
Update your last wrong commit message with new commit message in one line:
Or, try git reset like below:
Using reset to split commits into smaller commits
Here you have successfully broke your last commit into two commits. |
|||||||||
|
|
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 |
|||||
|
AmendYou have a couple of options here. You can do
as long as it's your last commit. Interactive rebaseOtherwise if it's not your last commit you can do an interactive rebase,
Then inside the interactive rebase you simply add edit to that commit. When it comes up do a |
||||
|
|
|
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 use the Git GUI as much as I can, and that gives you the option to amend the last commit:
Also, |
|||||
|
|
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
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
|
|||||
|
|
|||||
|
|
You also can use git
It's not as easy as a trivial Note that this will try to rewrite EVERY commit between |
|||||||||||||||||||||
|
|
I have added the alias of
|
||||
|
|
|
As already mentioned,
|
||||
|
|
|
|||||||||||||||||||||
|
|
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? |
|||||||||||||||||||||
|
|
Use
To understand it in detail, an excellent post is 4. Rewriting Git History. It also talks about when not to use |
|||||||||||||
|
|
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 are using the Git GUI, you can amend the last commit which hasn't been pushed with:
|
||||
|
|
|
I prefer this way.
Otherwise, there will be a new commit with a new commit ID |
|||||||||||||||||||||
|
Thank you for your interest in this question.
Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?
HEADorheadcan now use@in place ofHEADinstead. See this answer (last section) to learn why you can do that. – Cupcake Jul 26 '13 at 2:04