How to undo the last Git commit and keep changes staged?

To undo the last commit and keep the changes staged in Git, you can use the following steps:

  1. Run the command git reset --soft HEAD~1. This command will reset the HEAD pointer to the previous commit, effectively undoing the last commit. The --soft option ensures that the changes made in the undone commit are preserved and remain staged.

  2. After running the command, you can verify that the changes are still staged by executing git status. You will see the changes listed as staged modifications.

  3. If needed, you can make further modifications or add new changes to the staged area.

  4. Finally, commit the changes again using git commit -c ORIG_HEAD. This allows you to edit the commit message if desired.

By following these steps, you can undo the last commit while keeping the changes staged for further modifications before making a new commit.