Undoing the Last Commit in Git
To undo the last commit in Git while keeping the changes in your working directory, you can use the git reset command with the --soft option.
Why git reset --soft?
The --soft option will undo the last commit, but it will keep your changes in the staging area. This means you won't lose any of your work.
Step-by-Step Solution
Here's how you can do it:
This command will:
- Undo the last commit
- Keep your changes in the staging area
- Allow you to add more files or edit the commit message
Alternative Solution
Alternatively, you can also use git reset --soft HEAD^ to achieve the same result.
What to Do Next
After running the git reset --soft command, you can:
- Add more files to the staging area using
git add - Edit the commit message using
git commit --amend - Commit your changes again using
git commit
Remember to replace HEAD~1 or HEAD^ with the actual commit hash if you want to undo a specific commit that's not the most recent one.
Example Use Case
Let's say you committed too early with the message "WIP" and you want to add more files to the commit:
By using git reset --soft, you can safely undo the last commit and keep your changes in the working directory.