📜  git undo commit keep changes - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:41:28.057000             🧑  作者: Mango

Git Undo Commit Keep Changes

As a programmer, you may often find yourself in a situation where you've made a commit in Git, but realize you want to undo that commit while keeping your changes. This can be done using Git's reset command, combined with the --soft option.

Resetting a Commit with --soft

The git reset command can be used to move the HEAD pointer to a previous commit, effectively undoing the most recent commit. When combined with the --soft option, this will undo the commit without undoing the changes made in that commit.

git reset --soft HEAD~1

This command will move the HEAD pointer back one commit (HEAD~1), while keeping the changes made in that commit.

Committing the Changes Again

Once you have reset the commit, you can then make any additional changes necessary and commit them again.

git add .
git commit -m "New commit message"

This will stage and commit any changes made since the previous commit, with a new commit message.

Conclusion

Using git reset --soft is a useful technique for undoing a commit while keeping your changes. By combining this with normal Git commands for staging and committing changes, you can easily modify your repository history without losing any progress.