DEV Community

Cover image for How to Undo Pushed Commits with Git

How to Undo Pushed Commits with Git

Rizèl Scarlett on April 05, 2022

Introduction One major benefit of version control is that you can roll back your code to any time in history without significantly disru...
Collapse
 
devsmitra profile image
Rahul Sharma

I generally do this

// Go to previous commit
git reset --hard HEAD~1

// Force push
git push -f 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
vinceamstoutz profile image
Vincent Amstoutz

You shoud never force

Collapse
 
alco profile image
Jakub Stibůrek

Why not? The option is there.

Thread Thread
 
blackgirlbytes profile image
Rizèl Scarlett

Here’s an article on why it’s sometimes dangerous : blog.developer.atlassian.com/force...

Thread Thread
 
alco profile image
Jakub Stibůrek

Thanks, I'll have a look. But I reacted more to the "never".

Thread Thread
 
blackgirlbytes profile image
Rizèl Scarlett

Gotcha..I agree..never is a little strong lol. There's exceptions.

Thread Thread
 
christiankozalla profile image
Christian Kozalla • Edited

Although git is a distributed VCS and the client repositories are more or less independent, it is most common that the one remote repository is treated as the single source of truth.

Force pushing to that remote repo is essentially like saying 'screw what others might have pushed before, my local copy is the new single source of truth now'

The king is dead, long live the king!

Thread Thread
 
blackgirlbytes profile image
Rizèl Scarlett

You explained that pretty well! Going to refer to your response every time someone questions why I’m not using git push force

Collapse
 
vineetgnair profile image
Vineet G Nair

Me too. However this article is a new learning..

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett • Edited

Nice! Sometimes, I find myself taking that approach as well. I used the one in my article because it's safe and hopefully easy for folks to understand.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

If you're working with others, I'd recommend git push --force-with-lease to avoid accidentally deleting someones changes.

Collapse
 
bronaz profile image
BroNaz

It is considered good practice to disable force pushing. Often it's just banned.

Collapse
 
insidewhy profile image
insidewhy

It's considered good practice to have readable commit logs, and force pushing is essential for that task.

Collapse
 
spo0q profile image
spO0q

Thanks for the revert approach instead of the hazardous force push 🙏🏻 some people might use to "cover" their mistakes. It often creates more problem than it solves, while revert is perfectly readable and safe.

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

yeah thank you and I agree. Sometimes there's situations where a force push or "cover up" is needed..maybe for something like environment variables, but I agree that this method is easy to follow.

Collapse
 
spo0q profile image
spO0q

@blackgirlbytes the problem is who's using it. Every time someone tries to manipulate the history and overwrite stuff, it's a bit risky, by nature. Even if git experts might have appropriate usages of the command, I prefer disabling it as a general rule (e.g., in GitHub, you can protect branches against force push).

If you work alone, you do what you want, including rewriting history, but at your own risks if you damage your repository. If you work in a team, that's not acceptable because you might mess with other's work.

Thread Thread
 
blackgirlbytes profile image
Rizèl Scarlett

You’re absolutely right

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
Sloan, the sloth mascot
Comment deleted
 
Sloan, the sloth mascot
Comment deleted
 
Sloan, the sloth mascot
Comment deleted
 
blackgirlbytes profile image
Rizèl Scarlett • Edited

Hey @nenadj ! Happy to have your thoughts here, but I prefer a more positive, kind conversation. I’m not a fan of being dismissive.

Comments like, “Learn everything about git and you won’t have a problem” isn’t helpful to anyone lol. I work at GitHub, and I don’t know everything about Git.

Here are some thoughts on why rewriting history may be dangerous: stackoverflow.com/questions/149100...

Thread Thread
 
nenadj profile image
Nenad

:D

Thread Thread
 
blackgirlbytes profile image
Rizèl Scarlett

Thank you so much :)

Collapse
 
racheal profile image
Racheal Walker

hey, this is what i do.

Undo Pushed Commits in Git With Reset and Revert
Undo Pushed Commits With the git reset Command.
Undo Pushed Commits With the git revert Command.
Undo Pushed Commits With the git checkout Command.

Collapse
 
diballesteros profile image
Diego (Relatable Code)

Git revert is definitely a nice option. I would love a more in-depth article and look at git rebase and all it's options

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

More content on git rebase is on the way!

Collapse
 
diballesteros profile image
Diego (Relatable Code)

Good stuff!

Collapse
 
renanfranca profile image
Renan Franca

Congratulations 🎉! That's a helpful article👏
Normally my approach is to a git rebase, but there are some cases where the revert is much more simple 🤩

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

Thank you! Yeah, that's a common approach..not a wrong approach at all. I found this method is easy for me to explain and for others to understand.

Collapse
 
renanfranca profile image
Renan Franca

You are welcome! I do agree 💯👏

Collapse
 
vineetgnair profile image
Vineet G Nair

So to have multiple commits reverted, we have to do git revert multiple times ? If so I assume from the top commit. Any insights on that?

Collapse
 
eberjoe profile image
Eber Rodrigues

As the article mentioned, you can revert multiple commits by reverting an entire branch or a tag. And no, you don’t need to start from the top. You can pick any commit or group of commits in the repo’s history to revert.

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

Thanks @eberjoe ! That's correct. @vineetgnair I don't go too deep into it, but you can use any git revision with git revert.

You don’t have to use the commit hash to identify the commit you want to revert. You can use any value that is considered a gitrevision, including the:
Tag
Branch
Hash
Reference

And yes, you don't need to start from the top. I just start from the top in my example.

Collapse
 
tmchuynh profile image
Tina Huynh

Love the article! It's very informative

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

Thank you!!!

Collapse
 
acesif profile image
Asif Zubayer Palak

Very informative and easy to digest!

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

Thank you!!!

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

Completely agree. Force push can get a little tricky for folk, but I can see situations where it's needed.

Collapse
 
subhashsurana profile image
Subhash Surana

Excellent article. Recently, during an interview I was asked the difference between git revert & git reset and I did previously soft reset to revert the commit before even pushing the changes to remote repo. This would even work after we had pushed the changes. 🎉🎊🥳

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

Awesome! Glad that it's helpful to you!

Collapse
 
franzkafkayu profile image
Yu FranzKafka

Usually I use git reset rather than git revert and then git push.I don't like git revert because it will break my commit log because the " revert"🙃

Collapse
 
saroj8455 profile image
Saroj Padhan

Thank you

Collapse
 
aaravrrrrrr profile image
Aarav Reddy

Good article, thank you.