How to Undo a Git Push Safely

Undoing a Git push can mean either reversing the pushed change or removing commits from a remote branch. The right method depends on whether other people use that branch. For shared history, use git revert. Rewrite a remote branch only when you own it or have coordinated with everyone who depends on it.

Practice Git workflows →

Before you undo a push

First identify the branch and commit you want to undo. Check your current branch, working tree, and recent history:

git status
git branch --show-current
git log --oneline --decorate -5

Do not continue with commands that move or rewrite history until you have protected any uncommitted work and confirmed the target commit. If the push contained a credential or secret, revoke or rotate it immediately; rewriting Git history alone cannot make an exposed secret safe.

Safest for a shared branch: revert the pushed commit

If teammates may have fetched or based work on the pushed commit, usually keep the shared history intact and add a new commit that reverses its changes:

git revert <commit>
git push

Replace <commit> with the commit ID you identified. If the unwanted change is the latest commit, you can use git revert HEAD. Review the new revert commit and run relevant checks before pushing.

If Git reports conflicts, resolve them, stage the resolved files, and continue or finish the revert as Git instructs. To learn more about choosing between these approaches, see how to undo a Git commit.

Remove a commit from your own remote branch

If the branch is yours and nobody else depends on its current history, you can move it back and update the remote with a lease-protected force push. For the latest commit, this example removes the commit while keeping its changes staged locally:

git reset --soft HEAD~1
git log --oneline --decorate -5
git push --force-with-lease origin <branch>

Replace <branch> with the remote branch you intend to update. The reset changes your local branch; --force-with-lease then asks the remote to accept that rewritten history only if its current state matches what your local repository expects. If another person has updated the remote, the lease should reject the push instead of silently overwriting their work. Fetch and coordinate before deciding what to do next.

A lease is a safeguard, not permission to rewrite a shared branch. Confirm the branch, inspect the outgoing history, and tell affected collaborators first. Avoid plain git push --force because it can overwrite remote commits without that lease check. Read the safe force-with-lease guide before rewriting a remote branch.

Which method should you use?

SituationUsually useWhy
Shared branch or uncertain who fetched itgit revertAdds a reversal without removing existing history.
Your branch, no collaborators depend on itreset and --force-with-leaseReplaces remote history after a lease check.
Push included a password, token, or keyRevoke or rotate it immediatelyRemoving a commit does not undo exposure.

What if you pushed several commits?

If several commits need to be undone on a shared branch, revert the intended commits rather than resetting the branch. For a consecutive range, Git can create a reversal for each commit:

git revert <oldest-commit>^..<newest-commit>

Inspect the range carefully before running this command: Git revision ranges are inclusive here because the start is the parent of the oldest commit. For complex histories or merge commits, review Git's revert options and your project workflow before proceeding. Reverting a merge commit requires choosing a mainline parent.

Common questions about undoing a Git push

Can I undo a Git push after someone has pulled it?

Yes. On a shared branch, use git revert to publish a new commit that reverses the unwanted changes. This avoids making collaborators reconcile rewritten history.

Does git revert remove the original commit?

No. It preserves the original commit and adds another commit that reverses its effect. The original remains visible in history.

Does force-pushing delete a commit permanently?

It updates a branch reference; it does not guarantee that the commit is erased everywhere. Other clones, references, backups, or hosting provider retention may still contain it.

What if I pushed a secret by mistake?

Revoke or rotate the exposed credential immediately, then follow your organization's incident process. Removing the secret from Git history may also be necessary, but it does not undo the exposure.

Learn Git Faster by Practicing

Reading is just the first step. Real learning happens when you practice.

Start Learning Git →