Loading...

Git Tutorial

Changing commits in git

Let's learn how to modify an already existing commit

Step1:Change the page and commit

Put an author comment on the page.

FILE: hello.html

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

RUN:

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

Step2: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: kwikl3arn ([email protected]) -->
<html>
  <head>
  </head>
  <body>
    <h1>Hello, World!</h1>
  </body>
</html>

Step3: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(-)

Step4:View history

RUN:

git hist

RESULT:

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

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.