How can I uncommit my last commit in git?

How can I uncommit my last commit in git?

Is it

git reset --hard HEAD or

git reset --hard HEAD^ ?

To undo your last commit in Git, you can use the command:

git reset --hard HEAD^

This will remove your last commit and reset the changes to the state before the commit. Use with caution, as it will discard all changes in the last commit.

To undo your last commit in Git, you have two options:

  1. Keep modified changes in your working tree: Use git reset --soft HEAD^. This will undo the last commit but keep your changes staged.

  2. Discard all changes in the last commit: Use git reset --hard HEAD^. This will remove your last commit and discard all changes in it. Use this option carefully, as it cannot be undone.

To revert a commit without losing your work, use the --soft flag with git reset:

git reset --soft HEAD^

Be cautious, as git reset --hard will remove both your last commit and any local modifications that haven’t been committed yet.

To use git reset --hard on Windows, you’ll need to quote the HEAD^:

git reset --hard "HEAD^"