Skip to content

Instantly share code, notes, and snippets.

@cutiko
Created November 22, 2019 18:24
Show Gist options
  • Save cutiko/0b1615c63504a940877541362cc51211 to your computer and use it in GitHub Desktop.
Save cutiko/0b1615c63504a940877541362cc51211 to your computer and use it in GitHub Desktop.
Git delete 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.

ORIGINAL did fork but search didn't helped me

@lindsay824
Copy link

@unblocked games 76 Really? It doesn't work for me

@anton-x-t
Copy link

Thank you, it works!

@RaitonGG
Copy link

this deleted my code....

@PATELNAMRATA
Copy link

it's work thank you..

@0x1h
Copy link

0x1h commented Mar 31, 2024

this deleted my code....

what where you expecting lmao???

@RaitonGG
Copy link

this deleted my code....

what where you expecting lmao???

that it would delete the last commit? as in uncommit files? not revert them? lmao?

@anton-x-t
Copy link

this deleted my code....

what where you expecting lmao???

that it would delete the last commit? as in uncommit files? not revert them? lmao?

The command you were looking for was git reset HEAD^ but I think you used git reset --hard HEAD^ so the uncommitted files were removed.

@RaitonGG
Copy link

RaitonGG commented Apr 2, 2024

this deleted my code....

what where you expecting lmao???

that it would delete the last commit? as in uncommit files? not revert them? lmao?

The command you were looking for was git reset HEAD^ but I think you used git reset --hard HEAD^ so the uncommitted files were removed.

thank you good sire

@abdolmaleamir
Copy link

mine says (more?) what should i do

@anton-x-t
Copy link

mine says (more?) what should i do

A quick google on git reset more showed me the answer. Google is your friend @abdolmaleamir .

You should probably do git reset HEAD~1.

Google link

Solution credit: thank you Adam Dymitruk

@nhatban25912345
Copy link

Thank you, it works!

@JanTheMan1010
Copy link

Becarfull: It destroyed my last work. I just wanted to uncommit the last commit to put it on another branch .. anyway.

Before every reset/rebase I do a copy of my current branch by doing
git branch my-backup-branch

Everyone of us learned something the hard way :)

@MiranDaniel
Copy link

To undo this run git reflog, copy the id of the commit and reset to it git reset --hard <id>

abc1234 (HEAD -> main) HEAD@{0}: reset: moving to HEAD^
def5678 HEAD@{1}: commit: Made some changes
ghi9101 HEAD@{2}: commit: Added a new feature

git reset --hard def5678

@mkj-sfr
Copy link

mkj-sfr commented Jul 24, 2024

Whheew, I thought I had lost some big changes in a local project. I wanted to change the commit msg, but forgot how (git commit --amend)

This function removes all the changes too! If you do this and want your commit back, you can do the following:

git reflog <- This will show all the changes you made, including the commit hash of the commit you removed git reset --hard COMMIT_HASH <- will restore your changes

Credits to: https://stackoverflow.com/a/21778

I love you...
Literally had a heart attack :))

@EmreKanbay
Copy link

I WILL NOT COPY AND PASTE A SINGLE LINE OF CODE BEFORE THINKING 3 MORE TIMES, I HAVE LOST ALL MY PROGRESS. me sad :(

@mkj-sfr
Copy link

mkj-sfr commented Aug 28, 2024

I WILL NOT COPY AND PASTE A SINGLE LINE OF CODE BEFORE THINKING 3 MORE TIMES, I HAVE LOST ALL MY PROGRESS. me sad :(

you can restore your lost commit! just read the thread :))

@yatendra2001
Copy link

worked thanks man!!

@lfoliveir4
Copy link

thaaaaanks!!!!

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