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.

@CookingWithCale
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

@TruncatedDinoSour
Copy link

thank fucking god,
I love you @dipeshjadam
you saved my ass so hard xD

@nmz787-intel
Copy link

a commit "deleted" in this way still shows up in git reflog. How do I COMPLETELY remove a commit?

@Joeppie
Copy link

Joeppie commented May 13, 2022

a commit "deleted" in this way still shows up in git reflog. How do I COMPLETELY remove a commit?

old commits that are not part of any branch anymore will get garbage collected, typically in 30 days or so. There's a good chance the commit is not there anymore @nmz787-intel. If you look up how the garbage collection is done, perhaps you can find a way to really hard-delete it.
I'd recommend against it though, and just let 'nature'/gc take its course https://stackoverflow.com/questions/1904860/how-to-remove-unreferenced-blobs-from-my-git-repository

@rabeeh003
Copy link

thank you

@MosesWangira
Copy link

Use these 2 following commands:

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

Thank you +1
tHANKS

@lbruun
Copy link

lbruun commented Oct 9, 2022

Formally, you would want to do enclose the argument in quotes, meaning "HEAD^" rather than HEAD^.

Why? Because some shells will interpret the ^ as a line continuation character. Not saying which one, but it begins with a 'W'. He-he.

@hafizirfanzeikh
Copy link

thank, best wishes

@inapeace0
Copy link

Formally, you would want to do enclose the argument in quotes, meaning "HEAD^" rather than HEAD^.

Why? Because some shells will interpret the ^ as a line continuation character. Not saying which one, but it begins with a 'W'. He-he.

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

👍 Thanks, @lbruun

@haxisfree
Copy link

To force to remove the last commit from git, use these 2 following commands:

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

that was perfect for me. thanks...

@sumitsingh98
Copy link

I wanted to uncommit my last commit. so i used
git reset --hard HEAD~1
git push origin -f
but last commit changes did not seen in my vscode

so i tried
git reset HEAD^
now also not getting what to do?

@pratik149
Copy link

To force to remove the last commit from git, use these 2 following commands:

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

Confirming that it still works in March 2023!

@AdebayoIbrahim
Copy link

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

Works Like magic !!!

@asishantonyiq
Copy link

To force to remove the last commit from git, use these 2 following commands:

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

Life Saver
Still working in May 2023

@BLAKT00F
Copy link

BLAKT00F commented Jun 8, 2023

where do i put this code? in my terminal? I need to remove like my last 10 commits smh

@TruncatedDinoSour
Copy link

where do i put this code? in my terminal? I need to remove like my last 10 commits smh

yes, those commands are provided to be used with git cli, so u just run them in ur terminal

@BLAKT00F
Copy link

where do i put this code? in my terminal? I need to remove like my last 10 commits smh

yes, those commands are provided to be used with git cli, so u just run them in ur terminal

thank you my friend

@veerHash
Copy link

veerHash commented Aug 2, 2023

I am still seeing last two commits after used command: git reset --hard HEAD^

This works for me: git reset HEAD~

This works for me Thanks.

@AhmedMaad
Copy link

Formally, you would want to do enclose the argument in quotes, meaning "HEAD^" rather than HEAD^.

Why? Because some shells will interpret the ^ as a line continuation character. Not saying which one, but it begins with a 'W'. He-he.

Thanks a ton!

@Newbclharri
Copy link

Newbclharri commented Aug 18, 2023

what does -f mean in:
git push origin -f ??????????????????????

@Joeppie
Copy link

Joeppie commented Aug 18, 2023

@Newbclharri git push origin -f means the same as --force , it means it will push, even when the history doesn't match, or even when you have DELETED commits. Do not run this command unless you understand what it does, and even then, always prefer --force-with-lease, that way if two people were to run the same command at the same time, it would prevent the second person being able to accidentally overwriting and thus removing your commits from the branch.

@xebelge
Copy link

xebelge commented Aug 24, 2023

To force to remove the last commit from git,
use these 2 following commands:

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

Worked like a charm 👍😍

Gj, thx :)

@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.

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