Skip to content

Changing your last commit

Abhik B Pramanik edited this page Aug 11, 2011 · 2 revisions

If you want to add changes to your previous commit or change the previous message use git commit --amend. If you amend a commit that's already been pushed to the remote you'll have to force the next push to the remote (be careful as you may lose history).

  1. Let's make some changes:

    % echo "Changing stuff" >> INSTALL
    % git add INSTALL
    % git commit -m "Changing stuff"
    [master 86cc1c2] Changing stuff
    1 files changed, 1 insertions(+), 0 deletions(-)
    % git status
    # On branch master
    # Your branch is ahead of 'origin/master' by 1 commit.
    #
    nothing to commit (working directory clean)
    % git log
    commit 86cc1c2fc46f632a0ee4b4b0b97adf7f7f4d7c72
    Author: Abhik B Pramanik <abhik@alum.berkeley.edu>
    Date:   Wed Aug 10 20:45:48 2011 -0700
    
        Changing stuff
    
        commit 288741dc5ef74ba119eb75daa962da29b0c686a8
        Author: Abhik Pramanik <abhik@alum.berkeley.edu>
        Date:   Tue Aug 2 21:50:56 2011 -0700
    
            Adding clean instructions to INSTALL
    

Change the message

  1. With no staged changes change simply run git commit --amend or git commit --amend -m <message>

    % git commit --amend -m "Test"
    [master f3d1b09] Test
     1 files changed, 1 insertions(+), 0 deletions(-)
    % git status
    # On branch master
    # Your branch is ahead of 'origin/master' by 1 commit.
    #
    nothing to commit (working directory clean)
    % git log
    commit f3d1b095d7292f315a9fe9a72bb88cf60a19d7a2
    Author: Abhik B Pramanik <abhik@alum.berkeley.edu>
    Date:   Wed Aug 10 20:45:48 2011 -0700
    
        Test
    

Add changes

  1. Stage the commits you want to add and run git commit --amend

    % echo "Another" >> README
    % git add README
    % git commit --amend -m "Another"
    [master c6cfd32] Another
    2 files changed, 2 insertions(+), 0 deletions(-)
    % git status
    # On branch master
    # Your branch is ahead of 'origin/master' by 1 commit.
    #
    nothing to commit (working directory clean)
    % git log
    commit c6cfd32a559352a4bac9bf1dfc21ca7e726821d8
    Author: Abhik B Pramanik <abhik@alum.berkeley.edu>
    Date:   Wed Aug 10 20:45:48 2011 -0700
    
        Another
    

Clone this wiki locally