How to do it...

First, we'll try to undo the latest commit in the repository as though it never happened:

  1.  We'll make sure that our working directory is clean, no files are in the modified state, and nothing is added to the index:
$ git status 
On branch master 
Your branch is up-to-date with 'origin/master'. 

nothing to commit, working directory clean 
  1. Also, check what is in our working tree:
$ ls 
HelloWorld.java Makefile        hello_world.c 
  1. If all works well, we'll check the log to see the history of the repository. We'll use the --oneline switch to limit the output:
$ git log --oneline 
3061dc6 Adds Java version of 'hello world' 
9c7532f Fixes compiler warnings 
5b5d692 Initial commit, K&R hello world 
  1. The most recent commit is the 3061dc6 Adds Java version of 'hello world' commit. We will now undo the commit as though it never happened, and the history won't show it:
$ git reset --hard HEAD^ 

HEAD is now at 9c7532f Fixes compiler warnings 
  1. Check the log, status, and filesystem, so that you can see what actually happened:
$ git log --oneline 
9c7532f Fixes compiler warnings 
5b5d692 Initial commit, K&R hello world 

$ git status 
On branch master 
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded. 

  (use "git pull" to update your local branch)
 
nothing to commit, working directory clean 

$ ls 
hello_world.c 
  1. The commit is now gone, along with all the changes it introduced (Makefile and HelloWorld.java).
In the last output of the git status command, you can see that our master branch is one behind origin/master. This is similar to what we mentioned at the beginning of the chapter, because we are removing and undoing commits that are already published. Also, as mentioned, you should only perform the undo and redo (git reset) operations on commits that are not shared yet. Here, we only show it on the published commits to make the example easy to reproduce.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.66.13