git commit --amend

April 17, 2011

From the git-commit man page:

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

    You should understand the implications of rewriting history if you amend a
    commit that has already been published. (See the "RECOVERING FROM UPSTREAM
    REBASE" section in git-rebase(1).)

This is the simplest way to fix the last commit you made. The following alias makes it even easier:

$ git config --global alias.amend 'commit --amend'

Here it is in action:

evadeflow@SERENITY ~/Desktop/onekeywonder (master)
$ git commit -am "Update REAME"
[master 5ec5364] Update REAME
 1 files changed, 1 insertions(+), 0 deletions(-)

evadeflow@SERENITY ~/Desktop/onekeywonder (master)
$ git amend -m "Update README"
[master 6a65f82] Update README
 1 files changed, 1 insertions(+), 0 deletions(-)

Stick that in your git crackpipe and smoke it!

Leave a Comment

Previous post:

Next post: