Why does git push get rejected with 'non-fast-forward', and what should you do?
The remote branch has commits you do not have, so your push would overwrite them. Pull (or fetch and rebase) first to combine the work, then push again. Do not force-push over shared work.
Explain it simply
Two people edit the same Google Doc offline. The first to save wins; the second is told to refresh and merge before saving.
Git refuses a push if it cannot simply add your commits on top of the remote history. Someone else pushed first. Fix: git pull --rebase (or git pull), resolve any conflicts, and push again. git push --force would delete their commits from the remote, which is why it is dangerous. If you rewrote your own branch on purpose, use --force-with-lease, which refuses when the remote has changed unexpectedly.
Example
git push
# ! [rejected] main -> main (fetch first)
git pull --rebase
git pushWhat to say in the interview
"A non-fast-forward rejection means the remote has commits I lack. I pull with rebase or merge, resolve conflicts, and push again. I avoid --force on shared branches, and use --force-with-lease only on my own rewritten branches."
Common mistakes
- Reaching for git push --force as the first fix.