19. Changing commits

Goals

  • To learn how to modify an already existing commit

01 Change the page and commit

Put an author comment on the page.

File: hello.html

<!-- Author: Alexander Shvets -->
<html>
  <head>
  </head>
  <body>
    <h1>Hello, World!</h1>
  </body>
</html>

Run:

git add hello.html
git commit -m "Add an author comment"

02 Oops... email required

After making the commit you understand that every good comment should include the author's email. Edit the hello page to provide an email.

File: hello.html

<!-- Author: Alexander Shvets (alex@githowto.com) -->
<html>
  <head>
  </head>
  <body>
    <h1>Hello, World!</h1>
  </body>
</html>

03 Change the previous commit

We do not want to create another commit for adding the e-mail address. Let us change the previous commit and add an e-mail address.

Run:

git add hello.html
git commit --amend -m "Add an author/email comment"

Result:

$ git add hello.html
$ git commit --amend -m "Add an author/email comment"
[master 6a78635] Add an author/email comment
 1 files changed, 2 insertions(+), 1 deletions(-)

04 View history

Run:

git hist

Result:

$ git hist
* 6a78635 2011-03-09 | Add an author/email comment (HEAD, master) [Alexander Shvets]
* fa3c141 2011-03-09 | Added HTML header (v1) [Alexander Shvets]
* 8c32287 2011-03-09 | Added standard HTML page tags (v1-beta) [Alexander Shvets]
* 43628f7 2011-03-09 | Added h1 tag [Alexander Shvets]
* 911e8c9 2011-03-09 | First Commit [Alexander Shvets]

The new "author/email" commit replaces the original "author" commit. The same effect can be achieved by resetting the last commit in the branch, and recommitting new changes.