I wrote the wrong thing in a commit message. Alternatively, I've forgotten to include some files.
How can I change the commit message/files? The commit has not been pushed yet.
I wrote the wrong thing in a commit message. Alternatively, I've forgotten to include some files.
How can I change the commit message/files? The commit has not been pushed yet.
If you only want to change your last message you should use the
--only
flag or its shortcut-o
withcommit --amend
:This ensures that you don't accidentally enhance your commit with staged stuff. Of course it's best to have a proper
$EDITOR
configuration. Then you can leave the-m
option out, and git will pre-fill the commit message with the old one. In this way it can be easily edited.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.
pick
toreword
(or on old versions of Git, toedit
) in front of any commits you want to fix.For each commit you want to reword, Git will drop you back into your editor. For each commit you want to edit, Git drops you into the shell. If you’re in the shell:
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?
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.
Use
To understand it in detail, an excellent post is 4. Rewriting Git History. It also talks about when not to use
git commit --amend
.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.
You can use git-rebase-reword
It is designed to edit any commit (not just last) same way as
commit --amend
It is named after the action on rebase interactive to amend a commit: "reword". See this post and man -section interactive mode-
Examples: