Fix git push rejected (non-fast-forward) after amending a pushed commit
The problem
I amended a commit that had already been pushed, and the next push died with:
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'github.com:org/repo.git'
hint: Updates were rejected because the tip of your current branch is behind its remote counterpart
What didn't work
git pullthen push — creates a merge commit containing BOTH your amended commit and the original, so the change appears twice in history.git push --forceon reflex — if a teammate landed anything onmainsince your fetch, you just erased their commit.- Deleting the branch and re-pushing — same rejection, plus now the open PR is detached from the branch.
The fix
Rebase your rewritten history onto the remote tip, then push normally:
git fetch origin
git rebase origin/main
# conflicts? resolve, then: git add <files> && git rebase --continue
git push origin main
If you truly intend to overwrite remote history (e.g. nobody else has pulled), force safely:
git log --oneline origin/main..HEAD # confirm every commit here is YOURS
git push --force-with-lease origin main
Why it works
git rebase origin/main replays your amended commit on top of the remote's newest commit, so the push becomes a fast-forward again; --force-with-lease still fails if the remote moved between your fetch and your push, which is the exact scenario where a blind --force destroys a teammate's work.