Git - Amending Commits

From EdWiki

GitLab - Amending Commits

  • Goals
    Learn how to amend an existing commit
  • Change the program then commit
    Add an author comment to the program.
  • File : hello.c
    Author  : J.Shankarappa
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
    if( argc > 1 ) {
        puts("Hello ");
        puts(argv[1]);         /* prints !!!Hello argv[1] */
    } else {
        puts("Hello World!!!"); /* prints !!!Hello World!!! */
    return EXIT_SUCCESS;
git add hello.c
git commit -m "Add an author comment"
  • Oops, Should have an Email

After you make the commit, you realize that any good author comment should have an email included. Update the hello program to include an email.

    Author  : J.Shankarappa (
  • Amend the Previous Commit

We really don’t want a separate commit for just the email. Let’s amend the previous commit to include the email change.

git add hello.c
git commit --amend -m "Add an author/email comment"
  • Review the History
git hist

We can see the original “author” commit is now gone, and it is replaced by the “author/email” commit. You can achieve the same effect by resetting the branch back one commit and then recommitting the new changes.