To create a new commit that undoes the last commit:
git revert HEAD
To revert a specific commit (replace COMMIT_HASH):
git revert COMMIT_HASH
Soft Reset
To undo the last commit but keep changes staged:
git reset --soft HEAD^
To unstage changes as well:
git reset --mixed HEAD^
To discard changes entirely:
git reset --hard HEAD^
Stashing Changes
To stash changes:
git stash
Applying Stashed Changes
To apply stashed changes:
git stash apply
Clearing Stashes
To clear stashes:
git stash clear
This section covers undoing changes in Git, both at the commit level using git revert and git reset, and at the working directory level using git stash. Understanding these commands is essential for managing mistakes and refining the commit history.