DEV Community

foadlind
foadlind

Posted on • Originally published at practicalgit.com

Want to remove/delete your last commit? Here is how.

1. To remove a commit done locally:

You made a commit and then realized you want to remove it. But, you still want to keep your changes. This is achieved by:

$ git reset --soft HEAD^
Enter fullscreen mode Exit fullscreen mode

HEAD^ means go back one commit from where HEAD is now. This will get you to the state just before the last commit.
If you do a git status you will see that your changes are there just as they where before you staged them.

2. To remove a commit you have already pushed:

This is slightly tricky. There are safe and unsafe ways of doing this. Some of these unsafe ways involve changing the history of the repo and will create problems for other developers working with you in the same repo. I do not recommend those methods.

The safest way of removing a commit from remote is to revert the bad commit. Find the commit hash and:

$ git revert <commit-hash>
Enter fullscreen mode Exit fullscreen mode

This creates a new commit that undos the changes made in the bad commit. Now push this to remote and you are good to go.

signup

Top comments (12)

Collapse
 
dawidtabak profile image
Dawid Tabak

You could slightly shorten the first command with

git reset --soft HEAD^

This is especially handy when using git in powershell cli on windows, where you'd otherwise have to put 'HEAD@{1}' inside quotes.

Collapse
 
rad_hombre profile image
Matthew Orndoff

Let's go even shorter 🤤

git reset --soft @^
Collapse
 
foadlind profile image
foadlind

Yes you are right. I will update the article. Thanks.

Collapse
 
danielpdev profile image
danielpdev

Great post.
You could also use git reset --hard HEAD~1 to remove the last commit, be aware that it will also remove all of your uncommitted changes.

Collapse
 
darshanraval profile image
Darshan Raval

Hey It's Nice. It's very useful for me. 😁

Collapse
 
mikedubcurry profile image
Michael Curry

say I push some api secrets to the repo. Would the second method overwrite the commit or will,say my .env file still be accessible?

Collapse
 
mikedubcurry profile image
Michael Curry

When I started out, I left my .env files out of .gitignore for the majority of a project and had to resort to starting a fresh repo. Was that necessary if the commits were already so far back in the history?

Collapse
 
lt0mm profile image
Tom

As I understand you can use interactive rebase and then push force. I used this way and I could not find later any mention of my secret word, I hope somebody corrects me if I'm wrong

git rebase -i {prev_commit_hash}
git push -f

PS Interactive rebase is a big topic itself, but to delete commit it's enough to write "d" or "drop" in commit line which you want to delete

Thread Thread
 
mikedubcurry profile image
Michael Curry

Oh ok. At the time I ended up deleting the repo and copy pasting my files in with an updated .gitignore. Good to know there’s a better alternative

Collapse
 
nollidnosnhoj profile image
Dillon Johnson

Nice. Thanks for the tip! Very useful!

Collapse
 
hoxtygen profile image
Idowu Wasiu

Thanks for this. I'm planning on having git command collection so I don't have to do a Google search each time I need to do something sinister with git. This one is handy.

Collapse
 
maedox profile image
Pål Nilsen

I recently started using the Github desktop app and found that it has an undo button.