DEV Community

Cover image for How to undo a git push
Sankeeth Sriranganathan
Sankeeth Sriranganathan

Posted on

How to undo a git push

Programmers, even the best, can make mistakes. They might upload unfinished code online by accident, to the wrong place, or even share private info.

Mistake made: Pushed unwanted code online

Solution: Use the git reset command to remove the unwanted changes

  1. Use git log to view your commit history and find the bad commit (like a rewind button for your code history).
  2. Run git reset --soft <commit-hash> to point your branch back to a specific commit.

Two ways to "undo" a pushed commit:

  • git reset --soft: Keeps your changes and adds them to be committed later.
  • git reset --hard: Completely removes the changes from your code.

Pushing the fix (optional):

After "undoing" the mistake, you can push the changes again using git push --hard. Be careful! This rewrites history online, so only do this if everyone agrees.

Top comments (0)