How to Undo Last Commit and Keep Changes

andyrewlee

Andrew Lee

Posted on April 30, 2020

How to Undo Last Commit and Keep Changes

To undo the last commit but keep the changes, run the following command:

git reset --soft HEAD~1    
Enter fullscreen mode Exit fullscreen mode

Now when we run git status, we will see that all of our changes are in staging. When we run git log, we can see that our commit has been removed.

If we want to completely remove changes in staging, we can run the following command:

git reset --hard HEAD
Enter fullscreen mode Exit fullscreen mode

These are dangerous commands and should be used with caution. Try it out on a different branch first. However, in the worst case scenario, we can recover commits we accidentally deleted with git reflog.

💖 💪 🙅 🚩
andyrewlee
Andrew Lee

Posted on April 30, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related