One nice feature of git’s distributed nature is that if you haven’t pushed a commit to another repository, you can actually undo the commit, erasing it from git’s history. This is nice if you screwed up a commit and want to redo it before sharing it with the world. It’s simple to do too. The command line excerpt below demonstrates initializing a new repository, adding a file, editing it, undoing a commit, fixing a typo, and recommitting it…

One nice feature of git’s distributed nature is that if you haven’t pushed a commit to another repository, you can actually undo the commit, erasing it from git’s history. This is nice if you screwed up a commit and want to redo it before sharing it with the world. It’s simple to do too. The command line excerpt below demonstrates initializing a new repository, adding a file, editing it, undoing a commit, fixing a typo, and recommitting it.


  hike $ git init
  Initialized empty Git repository in .git/
  hike $ touch a_file.txt
  hike $ git add a_file.txt 
  hike $ git commit -m "adding a_file.txt" 
  Created initial commit ce29e03: adding a_file.txt
   0 files changed, 0 insertions(+), 0 deletions(-)
   create mode 100644 a_file.txt
  hike $ git status
  # On branch master
  nothing to commit (working directory clean)
  hike $ echo "a tpo" > a_file.txt
  hike $ git status
  # On branch master
  # Changed but not updated:
  #   (use "git add <file>..." to update what will be committed)
  #
  #       modified:   a_file.txt
  #
  no changes added to commit (use "git add" and/or "git commit -a")
  hike $ git add a_file.txt 
  hike $ git commit -m "committing a typo" 
  Created commit 34b6834: committing a typo
   1 files changed, 1 insertions(+), 0 deletions(-)
  hike $ git status
  # On branch master
  nothing to commit (working directory clean)
  hike $ git log
  commit 34b68343bfbdec4c2890bc3a1143121d12367ac5
  Author: David Vollbracht <[EMAIL REDACTED]>
  Date:   Sun Jun 15 23:11:52 2008 -0400

      committing a typo

  commit ce29e03891e441e76f1c76295868c4ecbfcd1f18
  Author: David Vollbracht <[EMAIL REDACTED]>
  Date:   Sun Jun 15 23:11:02 2008 -0400

      adding a_file.txt
  hike $ git reset ce29e03891e441e76f1c76295868c4ecbfcd1f18
  a_file.txt: needs update
  hike $ git status
  # On branch master
  # Changed but not updated:
  #   (use "git add <file>..." to update what will be committed)
  #
  #       modified:   a_file.txt
  #
  no changes added to commit (use "git add" and/or "git commit -a")
  hike $ git log
  commit ce29e03891e441e76f1c76295868c4ecbfcd1f18
  Author: David Vollbracht <[EMAIL REDACTED]>
  Date:   Sun Jun 15 23:11:02 2008 -0400

      adding a_file.txt
  hike $ cat a_file.txt 
  a tpo
  hike $ echo "a typo" > a_file.txt 
  hike $ git add a_file.txt 
  hike $ git commit -m "no typo" 
  Created commit d1816ad: no typo
   1 files changed, 1 insertions(+), 0 deletions(-)
  hike $ git status
  # On branch master
  nothing to commit (working directory clean)
  hike $ git log
  commit d1816ad31d6496c04717a840b97a455f1fb16e74
  Author: David Vollbracht <[EMAIL REDACTED]>
Date:   Sun Jun 15 23:13:26 2008 -0400

    no typo

  commit ce29e03891e441e76f1c76295868c4ecbfcd1f18
  Author: David Vollbracht <[EMAIL REDACTED]>
  Date:   Sun Jun 15 23:11:02 2008 -0400

      adding a_file.txt
  hike $ cat a_file.txt 
  a typo
  hike $ 

The key here is git reset ce29e03891e441e76f1c76295868c4ecbfcd1f18, which resets the head of the current branch to the state specified by the commit guid. In this case we are rolling back one commit (34b68343bfbdec4c2890bc3a1143121d12367ac50) by specified the guid of the commit before it—the one representing the state we want the head to return to. You can see from excerpt that the offending commit is removed from the git log, but the results of the change are still in the working copy, waiting to be fixed and recommitted. I haven’t been using git very long, but I’ve already found this a useful recipe on a couple of occasions. I hope you will to.

Sorry, comments are closed for this article.