Published on November 14, 2024By DeveloperBreeze

To undo your last commit without affecting your working directory or staged changes (i.e., to keep your changes but remove the commit), you can use the following command:


git reset --soft HEAD~1

Explanation

  • HEAD~1 refers to the previous commit.
  • --soft keeps your changes in the staging area so you can recommit them if needed.

If You Want to Remove the Commit and Unstage Changes

If you want to undo the commit and also unstage the changes (but keep them in your working directory), use:


git reset HEAD~1

If You Want to Completely Discard the Commit and Changes

If you want to discard both the commit and the changes (be careful with this as it cannot be undone), use:


git reset --hard HEAD~1

These commands will remove the last commit, and since you haven’t pushed it yet, there won’t be any issues with the remote repository.

Comments

Please log in to leave a comment.

Continue Reading: