Sub topic 11
Fixing a Rejected Push
This chapter assumes you can commit, create branches, and tell a merge from a rebase. It also assumes you know that origin/main is a remote-tracking branch, which is your local record of where main was on the remote at your last fetch.
Why Git rejects a push
A push asks the remote to move a branch to your commit. The remote accepts only if that move is a fast-forward. That means the commit the remote branch points to must be an ancestor of the commit you are pushing.
If a teammate pushed to main after your last fetch, the remote tip is not in your history. Your histories have diverged. Accepting your push would drop their commit from the branch, so the remote refuses.
Reading the error
git push origin main
# ! [rejected] main -> main (fetch first)The text in parentheses tells you what is missing.
| Hint | Meaning |
|---|---|
| `fetch first` | The remote has commits you have never downloaded. |
| `non-fast-forward` | You have fetched them, but your branch does not contain them yet. |
Both are fixed the same way: get the remote commits into your history, then push again.
Fix 1: rebase onto the remote branch
Rebasing replays your local commits on top of the remote ones, which keeps history linear.
git fetch origin
git rebase origin/main
git push origin mainAfter the rebase, origin/main is an ancestor of main, so the push is a fast-forward. Your commits get new hashes. That is safe here because nobody else has them yet.
Fix 2: merge the remote branch
If your team prefers merge commits, or you do not want to rewrite your commits, merge instead.
git fetch origin
git merge origin/main
git push origin mainEither approach works. Follow the convention of your project.
Handling conflicts
If your changes and the remote changes touch the same lines, Git stops and reports a conflict. During a rebase, fix the files and continue.
# edit the conflicting files, then:
git add path/to/file
git rebase --continueTo undo the attempt and return to where you started, run git rebase --abort. For a merge, the equivalent is git merge --abort. Nothing has been pushed at this point, so aborting loses nothing.
What not to do
The tempting shortcut is git push --force. It makes the remote accept your branch and discards your teammate's commit C from that branch. Their work is not deleted from their machine, but the shared branch no longer has it, and the next person to push can cause more damage.
A safer routine
Fetch before you start a push, and look at what is incoming.
git fetch origin
git log --oneline main..origin/mainIf the log prints nothing, the remote has nothing new and your push will succeed. If it prints commits, integrate them first. Many people set git config --global pull.rebase true so that git pull does the fetch and rebase in one step. That reduces rejections but does not remove the need to understand them.
Exercise
You run git push origin main and see ! [rejected] main -> main (fetch first). A teammate says to run git push --force. What do you run instead, and why?
Show answer
Run git fetch origin, then git rebase origin/main (or git merge origin/main), resolve any conflicts, and run git push origin main again. The rejection means the remote has commits you do not have. Integrating them makes your push a fast-forward. A force push would overwrite the remote branch and remove your teammate's commits from it.
Exercise
During git rebase origin/main Git stops with a conflict in app.py. List the commands you run to resolve it and continue, and the command that abandons the rebase.
Show answer
Edit app.py to resolve the conflict, run git add app.py, then git rebase --continue. To abandon the rebase and return to your original branch state, run git rebase --abort.