# Delete last commit from git ## First way ```jsx git reset --hard HEAD~1 git push --force ``` ## Another way ``` git reset --hard "HEAD^" git push origin -f ``` Both ways works fine. --- # Recover deleted commit to git ## Step 1. Find full SHA of deleted commit ```jsx git fsck --lost-found Example output: Checking object directories: 100% (256/256), done. Checking objects: 100% (3/3), done. dangling commit 5601624a8926d4426ad42768bd0637fd82b70b80 dangling commit d5ee3e10a2c8b17b4e3cd7ee0562d6cdafacc5fa dangling commit 1b0b23f462cb4c560c3adfa821bcddd4e53e6a43 dangling blob b99c193caeccb77af13df8adb0358cee0b93eb82 ``` ## Step 2. Recover the dangling commit with rebase ```jsx git rebase 5601624a8926d4426ad42768bd0637fd82b70b80 git push ``` --- Deleted in this way commit still in **git reflog**. But garbage collector should remove typically in 30 days or so.