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

  • -f stands for "force" and tells git clean to remove untracked files.
  • -d means also remove untracked directories.
  • -x means also remove the files ignored by git, like those mentioned in .gitignore.

If you have multiple branches and you want all your local branches to match the remote branches, you can check out each branch and repeat the reset command:

Aug 09, 2024
Read More

Discussion 0

Please sign in to join the discussion.

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