Skip to content

Instantly share code, notes, and snippets.

@cutiko
Created November 22, 2019 18:24
Embed
What would you like to do?
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

@jainamsutaria123
Copy link

it works, thanks :)

@The-Estonian
Copy link

Thanks a ton for that one!

@ahmeddarafat
Copy link

great!

@Schultzlm
Copy link

I will try it now, let's hope it still works.

@demo-inv-riyas
Copy link

How can I remove a specific commit by using SHA or message in between all of the commits?

@cutiko
Copy link
Author

cutiko commented Nov 17, 2022

@demo-inv-riyas take a look at git rebase. The other idea would be

  1. Revert before the first commit to delete
  2. checkout to new branch
  3. cherry pick from the origin branch all the commits you need skipping the one you don't need

https://stackoverflow.com/a/39119678
https://stackoverflow.com/a/3933416

Good luck

@Me8848You
Copy link

Thanks it workss!!!

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