Fix or reset git commits with precision
Assuming the latest commit (HEAD in the current branch) has not been published with a git push, here are some ways to fix or reset it. In order from least to most destructive:
Amend changes to the last commit:
git add -A
git commit --amend
Undo the last commit but keep all changed files in the staging area:
git reset --soft HEAD~1
Undo the last commit but keep all changes in the working tree un-staged:
git reset HEAD~1
Undo the last commit and blow away changes to tracked files only:
git reset --hard HEAD~1
Undo the last commit and blow away everything including untracked changes:
git reset --hard HEAD~1
git clean -df
NOTE: Using these commands after the commit is published will cause issues for other users. Additionally, none of these commands remove the commit's history. This means you can still return to the original commit by using git reflog to find the commit's hash and using git reset --hard [hash] to restore the commit.
If the latest commit has been published but you still want to roll it back you can undo last commit by creating a new commit that reverses the changes:
git revert HEAD~1