tags:

views:

29797

answers:

5

I stupidly did a git commit while half asleep, and wrote totally the wrong thing in the commit message, How do I change the commit message? I have not yet pushed the commit to anyone

+120  A: 
git commit --amend

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.

Denis Bueno
+1 my all time favorite command in Git
Evan Plaice
However `git commit --amend` isnt as powerful as `git rebase -i`.
jeffjose
@jeffjose, It definitely doesn't need to be. Also, `git commit --amend` can fix up the (a?) master commit.
strager
A: 

The simplest way to just change the commit messages it to type

git reset --soft HEAD^
git commit -a -c ORIG_HEAD

Which will recommit the most recent commit, re-opening the edit message in your editor of choce for you to change as you want

Laurie Young
This is exactly what git commit --amend does, only longer.
Denis Bueno
Can you explain what those commands are doing? One thing I've noticed about git 'help' is that a specific problem will be solved by a sequence of commands, but what those commands do is not explained . .
Michael Donohue
+111  A: 

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

  1. git rebase --interactive $parent_of_flawed_commit

    If you want to fix several flawed commits, pass the parent of the oldest one of them.

  2. An editor will come up, with a list of all commits since the one you gave.

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

  3. 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:

    1. Change the commit in any way you like.
    2. git commit --amend
    3. 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.

Aristotle Pagaltzis
Can one change the message of the first commit (which doesn't have a parent)?
13ren
I too, want to change the text of the first commit.
Cylindric
life saver for me
drudru
This is mentioned in one of the other answers but I will put a note of it here. Since git 1.6.6 you can use `reword` in place of `pick` to edit the log message.
MitMaro
Thanks, MitMaro. I amended the answer to include that.
Aristotle Pagaltzis
life saver!!! writing a 100,000 line log message is not a good idea :)
Rob
+48  A: 

git commit --amend -m "your new message"

lfx_cool
This is the only reply (at time of this comment) that exactly answers the question!
Phil Nash
And it still is.
Mark Beckwith
This is the right answer!
rp
+6  A: 

The solution from Aristotle can be shortened since git 1.6.6 (suppressing step 3): in step 2.1, you can change pick to reword (instead of edit) and, once the file is saved, git will pop up your editor to modify each commit message.

arno