Skip to content

Instantly share code, notes, and snippets.

@CrookedNumber
Created February 12, 2014 21:02
Show Gist options
  • Save CrookedNumber/8964442 to your computer and use it in GitHub Desktop.
Save CrookedNumber/8964442 to your computer and use it in GitHub Desktop.
git: Removing the last commit

Removing the last commit

To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.

If you want to "uncommit" the commits, but keep the changes around for reworking, remove the "--hard": git reset HEAD^ which will evict the commits from the branch and from the index, but leave the working tree around.

If you want to save the commits on a new branch name, then run git branch newbranchname before doing the git reset.

@mostafa-bs23
Copy link

git reset --hard HEAD~ works for me but I used git reset HEAD~ removing flag --hard to keep the changes.

@andriyl
Copy link

andriyl commented Oct 3, 2023

git reset --hard HEAD~1
git push origin -f

works for me, tnx a lot

@bdmostafa
Copy link

git reset --hard HEAD~ works for me but I used git reset HEAD~ removing flag --hard to keep the changes.

It works for me. Thanks 👍

@hiddegrei
Copy link

I spent the past hour freaking out, I cried, I feel like less of a man, but figured out how to fix it, pending git has not collected the garbage yet.

git fsck --lost-found

dangling commit b72e67a9bb3f1fc1b64528bcce031af4f0d6fcbf

Recover the dangling commit with rebase:

$ git rebase b72e67a9bb3f1fc1b64528bcce031af4f0d6fcbf

damn thanks

@lamiaagabriel
Copy link

git reset --hard HEAD^
git push origin -f

Works Great.

@Ripple-Tech
Copy link

Ha ha, if you are reading this thread, you must have fucked up your repo.

@TruncatedDinoSour
Copy link

Ha ha, if you are reading this thread, you must have fucked up your repo.

false, i did it on Mar 29, 2022

@SeLub
Copy link

SeLub commented Feb 8, 2024

Remove last commit from GitHub

First way

git reset --hard HEAD~1
git push --force

Another way

git reset --hard "HEAD^"
git push origin -f

Both ways works fine.


Return removed commit to GitHub

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.

This was created thanks to you as a summary of the topic.
Thank you all.

@fmohamed1
Copy link

Use these 2 following commands:

git reset --hard HEAD^
git push origin -f

Thank you +1
tHANKS

@fmohamed1
Copy link

that's what I was looking for thank you so much

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