How to uncommit my last commit in Git?

 

To uncommit your last commit in Git, you can use the git reset command with the --soft option. This will remove the last commit while keeping the changes staged (i.e., in the "Changes to be committed" area). Here's how to do it with an example:

bash
# Make sure you are on the branch where you want to uncommit the last commit # Use 'git branch' to check your current branch and 'git checkout' to switch if needed # Uncommit the last commit while keeping changes staged git reset --soft HEAD~1

In this example:

  1. Make sure you are on the branch where you want to uncommit the last commit. You can use git branch to check your current branch and git checkout to switch to the correct branch if needed.

  2. Use the git reset command with the --soft option. HEAD~1 refers to the commit just before the last commit, effectively undoing the last commit while keeping the changes staged (but not committed).

After running this command, the last commit is removed from the branch, and the changes from that commit are in the "Changes to be committed" area, ready for you to make any modifications if needed. You can then commit the changes again with a new commit message or amend the previous commit using git commit --amend.

Comments