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~1Explanation
HEAD~1refers to the previous commit.--softkeeps 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~1If 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~1These commands will remove the last commit, and since you haven’t pushed it yet, there won’t be any issues with the remote repository.