DeveloperBreeze

How to Undo Your Last Commit Without Changing Your Working Directory

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.

Continue Reading

Discover more amazing content handpicked just for you

Note
bash

How to reset your local Git repository to match the remote repository exactly

If you want to make sure there are no leftover untracked files or directories in your working directory, you can use:

git clean -fdx

Aug 09, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!