Skip to content

Instantly share code, notes, and snippets.

@SeLub
Last active February 8, 2024 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeLub/f3d61ae628d500e022f28822ad611127 to your computer and use it in GitHub Desktop.
Save SeLub/f3d61ae628d500e022f28822ad611127 to your computer and use it in GitHub Desktop.
Remove or recover last commit pushed to git

Delete last commit from git

First way

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

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment