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.

Related Posts

More content you might like

Note
bash

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

Replace <branch-name> with the name of the branch you want to reset.

Sometimes, branches are deleted from the remote but they still exist in your local copy. To remove these stale branches from your local repository, you can use:

Aug 09, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!