0
0
GitHow-ToBeginner · 3 min read

How to Undo Last Push in Git: Simple Commands Explained

To undo the last push in Git, use git reset --soft HEAD~1 to keep changes locally and then force push with git push --force. This removes the last commit from the remote repository while keeping your work safe.
📐

Syntax

The main commands to undo the last push are:

  • git reset --soft HEAD~1: Moves the current branch back by one commit but keeps your changes staged locally.
  • git push --force: Updates the remote repository by overwriting the last pushed commit.

Use HEAD~1 to refer to the commit before the last one.

bash
git reset --soft HEAD~1
git push --force
💻

Example

This example shows how to undo the last pushed commit while keeping your changes safe locally.

bash
$ git log --oneline -n 3
abc1234 Fix typo in README
bcd2345 Add new feature
cde3456 Initial commit

$ git reset --soft HEAD~1
$ git status
On branch main
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
	modified: README.md

$ git push --force
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 300 bytes | 300.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To github.com:user/repo.git
 + bcd2345...abc1234 main -> main (forced update)
Output
abc1234 Fix typo in README bcd2345 Add new feature cde3456 Initial commit On branch main Changes to be committed: modified: README.md Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 4 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 300 bytes | 300.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0) To github.com:user/repo.git + bcd2345...abc1234 main -> main (forced update)
⚠️

Common Pitfalls

Common mistakes when undoing the last push include:

  • Not using --force with git push, so the remote does not update.
  • Using git reset --hard without backup, which deletes local changes permanently.
  • Undoing pushes on shared branches without informing teammates, causing confusion.

Always double-check before force pushing to avoid data loss.

bash
Wrong way (loses local changes):
git reset --hard HEAD~1
git push --force

Right way (keeps local changes):
git reset --soft HEAD~1
git push --force
📊

Quick Reference

CommandPurpose
git reset --soft HEAD~1Undo last commit but keep changes staged locally
git reset --hard HEAD~1Undo last commit and discard local changes (use with caution)
git push --forceForce update remote branch to match local state
git push --force-with-leaseSafer force push that checks for remote changes first

Key Takeaways

Use git reset --soft HEAD~1 to undo the last commit but keep your changes.
Always follow with git push --force to update the remote repository.
Avoid git reset --hard unless you want to discard local changes permanently.
Communicate with your team before force pushing to shared branches.
Consider git push --force-with-lease for safer force pushes.