Using Git reset to undo the last commit
 
  August 1, 2019

Sometimes we need to undo our last commit but we don't want to lose the changes we've already made. There are several reasons, sometimes we made a commit prematurely or we forgot to add a file, etc.

WARNING: These commands assume we haven't sync/publish the changes to a remote origin.

git reset: What it actually does is that moves the actual branch to X commit, so, using HEAD~1 or "HEAD^" (both are valid) we move the actual branch to the previous commit, example:

 git reset

            

\$git reset HEAD~1
            
            
        

If we included --soft as parameter it will mark the files (actual changes) ready to commit

 Using soft

            

\$git reset --soft HEAD~1
            
            
        

Ok then, as simple as that, but sometimes we require to clean up a little and reflect that on the commit message in that case use git commit --amend (Notice that this will open our configured editor to change the message), example:

 Use amend

            

$git rm private.key
$git commit --amend
            
            
        


Use case, You committed your code twice but noticed there is an error in the author

 Fix the author

            

$git reset HEAD~1
$git commit --amend --author="Jorge Anaya "
$git add .
$git commit -m "Fix bla bla bla"