DEV Community

Andrew Lee
Andrew Lee

Posted on

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.

Top comments (0)