← back to diagnosis

pushed bad commits

caution

You pushed commits to a remote and need to undo them. How bad this is depends on what you pushed and whether anyone else has pulled.

If you just pushed and need to revert the most recent commit:

$ git revert HEAD
$ git push origin <branch>

git revert creates a new commit that is the exact inverse of the target. Your history stays intact — nothing is deleted or rewritten.

i pushed secrets or credentials

danger

Reverting is not enough. The secret is in your git history and anyone with repo access can find it. You need to rotate the credentials immediately and scrub the history.

1. Rotate the exposed credentials immediately
Use BFG Repo-Cleaner to scrub the history:
$ bfg --delete-files .env
2. Or use git filter-branch (slower):
$ git filter-branch --force --index-filter ...
$ git push --force

Force pushing rewrites public history. Coordinate with your team first. Anyone who already pulled will need to re-clone or reset.

it wasn't the most recent commit

caution

Find the commit hash, revert it, then test before pushing. Others may have built on top of it.

$ git log --oneline
$ git revert <commit hash>

Rerun your tests before pushing. It's possible others wrote code depending on what you're reverting.

i force-pushed and overwrote someone's work

danger

The remote history was rewritten. Your teammate's commits may still exist in their local reflog. Coordinate immediately.

$ git reflog # on the affected teammate's machine
$ git reset --hard <lost-commit-hash>
$ git push --force-with-lease