git commit --amend # edit last commit
git commit --amend --no-edit # edit last commit w/o changing commit message
When you make a mistake (e.g. a hard reset to a previous commit, losing important work), you can always undo your changes made with git, even when it seems irrevocaly lost
# hard reset to `HEAD~1`, removing the last commit and all its content
$ git reset --hard HEAD~1
Using git reflog
you can see all the operations that were performed with git
, inclunding the hard reset. You can also go back before this seemingly unrecoverable state
$ git reflog
caa1d2605 (HEAD -> feat/advances) HEAD@{0}: reset: moving to HEAD~1
5f21c1713 (origin/feat/advances) HEAD@{1}: checkout: moving from master to feat/advances
6c41ebcea (master) HEAD@{2}: checkout: moving from feat/advances to master
$ git reset --hard 5f21c1713
This "undo" is also "undoable", as it appears in the git reflog
as well
$ git reflog
5f21c1713 (HEAD -> feat/advances, origin/feat/advances) HEAD@{0}: reset: moving to HEAD@{1}
caa1d2605 HEAD@{1}: reset: moving to HEAD~1
5f21c1713 (HEAD -> feat/advances, origin/feat/advances) HEAD@{2}: checkout: moving from master to feat/advances
6c41ebcea (master) HEAD@{3}: checkout: moving from feat/advances to master
$ git reset --hard caa1d2605 # you just "undid the undo", back to step 1 ;)