DEV Community

Discussion on: How to Undo Pushed Commits with Git

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
 
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
 
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
 
vineetgnair profile image
Vineet G Nair

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

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