How to do it...

  1. Check whether we have any files in the modified state and check the log:
$ git status 
On branch master 
Your branch is up-to-date with 'origin/master'.  

nothing to commit, working directory clean 

$ git log --oneline 
3061dc6 Adds Java version of 'hello world' 
9c7532f Fixes compiler warnings 
5b5d692 Initial commit, K&R hello world 
  1. Now, we can undo the commit, while retaining the changes in the index:
$ git reset --soft HEAD^ 

$ 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) 

Changes to be committed: 

 (use "git reset HEAD <file>..." ...

Get Git Version Control Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.