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 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/
.-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 underrefs/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
: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 yourmsg-filter
command very wise ;-)If you are using the Git GUI, you can amend the last commit which hasn't been pushed with:
If you only want to modify your last commit message, then do:
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 thegit rebase -i
command: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.