We earn commissions when you shop through the links below, at no extra cost to you. Our guides and technical recommendations are written independently of these relationships: a commission never decides what we recommend, how we diagnose a problem or what we publish, and the content does not depend on it. Affiliate disclosure

Git · Beginner · 12 min

Fix Git push rejected (non-fast-forward)

Fix git push rejected (fetch first, non-fast-forward): fetch, see how the branches diverged, rebase or merge, and force-push only with a lease.

Git refuses a push that would throw away commits on the remote. This runbook reads the rejection, shows how the branches diverged, joins them with a rebase or a merge, handles a conflict on the way, and explains when --force-with-lease is the right tool. Commands were run with Git 2.53.0 against a local bare repository.

See it run

A short replay of real output from commands in this guide, captured on a WSL2 Ubuntu test machine. The typing is replayed, and the host name, account and MAC addresses are redacted. Your output will differ, and a test machine is not a production server.

Text version of this demo
ops@web-01:~$ git push origin main
To ~/demo/origin.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to '~/demo/origin.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

ops@web-01:~$ git fetch -q && git status -sb | head -n 1
## main...origin/main [ahead 1, behind 1]

ops@web-01:~$ git log --oneline --graph --all
* 7f3538e Add alice.txt
| * 244ee99 Add bob.txt
|/  
* 7087d5c Initial commit

ops@web-01:~$ git pull --rebase origin main
From ~/demo/origin
 * branch            main       -> FETCH_HEAD
Successfully rebased and updated refs/heads/main.

ops@web-01:~$ git push origin main
To ~/demo/origin.git
   244ee99..bfe52f2  main -> main

git push only moves a branch on the remote forward. If the remote branch has commits that your local branch does not have, the push would throw them away, so Git refuses with ! [rejected]. The fix is to bring those commits into your branch first, by rebasing or merging, and then push again. This runbook reads the message, shows how the two branches diverged, joins them without losing anyone's work, and explains when a force push is acceptable.

Commands and output were captured with Git 2.53.0 on Ubuntu 26.04: two clones (alice and bob) of a local bare repository, with no personal Git configuration. We did not run this against GitHub, GitLab or any other hosting service, so their wording may differ. Section 6 covers rejections from the server itself, which were not reproduced here.

Why a push is rejected, and the two ways outWhy it is rejected: Your commit (on top of the old remote tip) then The remote has a newer commit (you have not fetched it) then Push refused (fetch first, or non-fast-forward) The usual fix: git fetch (get the remote commits) then Rebase or merge (your commits on top of theirs) then git push (a fast-forward now) Replacing the remote on purpose: git push --force-with-lease (only if the remote is where you last saw it) then Refused if someone pushed since (the lease protects their commit)Why it is rejectedYour commiton top of the old remote tipThe remote has a newercommityou have not fetched itPush refusedfetch first, ornon-fast-forwardThe usual fixgit fetchget the remote commitsRebase or mergeyour commits on top of theirsgit pusha fast-forward nowReplacing the remote on purposegit push --force-with-leaseonly if the remote is where you last saw itRefused if someone pushed sincethe lease protects their commit
The remote has a commit you do not have, so a plain push would throw it away. Join the histories and push again. Force only when you mean to replace the remote branch, and let the lease refuse if someone pushed since your last fetch.

0. Rules of engagement

  1. Do not force-push a shared branch to make the error go away. git push --force replaces the remote branch with yours and deletes your colleagues' commits from it.
  2. Commit or stash your work first. A rebase or merge with uncommitted changes either refuses to start or mixes them into the result.
  3. Check the branch name. git status -sb shows which local branch you are on and which remote branch it follows.

1. Read the message

Commit hashes in this runbook come from the test runs. Your hashes will differ; only the shape of the output matters.

# Real output from the test repository, as run: Bob pushed first, and Alice has not fetched since
$ git push origin main
To /tmp/gp/origin.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to '/tmp/gp/origin.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Reason in bracketsWhat it meansGo to
(fetch first)The remote has commits you have never fetchedSection 2
(non-fast-forward)You have fetched them, but your branch does not contain them. The hint says the tip of your current branch is behind its remote counterpartSection 2
(stale info)A --force-with-lease push refused because the remote moved since your last fetchSection 5
! [remote rejected] ... (...)The server refused for its own reasons: a protected branch, a hook, a ruleSection 6

The first two are the same problem seen before and after git fetch.

# Real output from the test repository after git fetch, as run: the same push, now with the other wording
$ git fetch origin
From /tmp/gp/origin
   7709111..c807979  main       -> origin/main
$ git push origin main
To /tmp/gp/origin.git
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to '/tmp/gp/origin.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

2. See how the branches diverged

# Get the remote commits without changing your branch, then compare
git fetch origin
git status -sb | head -n 1
git log --oneline --graph --all -n 15
# Real output from the test repository: one commit on each side since they split
## main...origin/main [ahead 1, behind 1]
* 4bde2f4 Add alice.txt
| * c807979 Add bob.txt
|/  
* 7709111 Initial commit
  • behind N only: you have nothing new. git pull --ff-only moves your branch forward, and there is nothing to push.
  • ahead N, behind M: both sides have new commits. Join them (section 3).
  • Much more behind than you expected, or commits you do not recognize: you may be on the wrong branch, or someone rewrote the remote branch. Ask before you rebase anything onto it.

To see exactly what the others pushed: git log --oneline HEAD..origin/main. To see what only you have: git log --oneline origin/main..HEAD.

3. Join the remote commits: rebase or merge

With current Git versions, a plain git pull on diverged branches stops and asks you to choose, unless pull.rebase or pull.ff is configured:

# Real output from the test repository, as run: git pull origin main with no configuration
From /tmp/gp/origin
 * branch            main       -> FETCH_HEAD
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

Rebase puts your commits on top of the remote ones and keeps the history in one line. Use it for commits that you have not pushed yet, which is the usual case here.

# Replay your local commits on top of the remote branch, then push
git pull --rebase origin main
git push origin main
# Real output from the test repository, as run (three commands in a row, their `$` lines left out): git pull --rebase origin main, git log --oneline --graph -n 5, git push origin main
From /tmp/gp/origin
 * branch            main       -> FETCH_HEAD
Rebasing (1/1)Successfully rebased and updated refs/heads/main.
* 11221c4 Add alice.txt
* c807979 Add bob.txt
* 7709111 Initial commit
To /tmp/gp/origin.git
   c807979..11221c4  main -> main

Your rebased commits get new IDs (4bde2f4 became 11221c4). That is fine as long as nobody else has them yet.

Merge (git pull --no-rebase origin main) keeps both lines and adds a merge commit. Use it when your commits are already shared, or when your team prefers merge commits. Either way, set a default once so git pull stops asking: git config --global pull.rebase true (or false).

git pull --ff-only is a safe check, not a fix: on diverged branches it refuses with fatal: Not possible to fast-forward, aborting. (exit code 128; in our run it printed the From/branch lines and a hint about git merge --no-ff and git rebase before that line) and did not rebase or merge.

4. A conflict during the rebase

When both sides changed the same lines, the rebase stops at that commit.

# Real output from the test repository, as run: Alice and Bob both edited app.txt (git pull --rebase origin main)
From /tmp/gp/origin
 * branch            main       -> FETCH_HEAD
Rebasing (1/1)Auto-merging app.txt
CONFLICT (content): Merge conflict in app.txt
error: could not apply e7f71b8... Alice edits app.txt
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Could not apply e7f71b8... # Alice edits app.txt
# Find the conflicted files and look at the markers
git status --short
git diff

<<<<<<< HEAD to ======= is the remote version (during a rebase, HEAD is the branch you are rebasing onto); ======= to >>>>>>> is your commit. Edit the file to the result you want, remove the markers, then:

# Mark the file as resolved and let the rebase go on
git add app.txt
git rebase --continue

In our run the resolve step was echo resolved > app.txt, git add app.txt, then GIT_EDITOR=true git rebase --continue (the variable only keeps the editor from opening for the commit message); the result was Successfully rebased and updated refs/heads/main. and ## main...origin/main [ahead 1], and the push went through.

Changed your mind halfway through? git rebase --abort returns your branch to where it was before the pull. In our run git status -sb showed ahead 1, behind 1 again and the branch tip had the same hash as before the pull.

5. When a force push is right, and the safe way to do it

After you rebase or amend commits that you had already pushed to your own branch (for example a feature branch with an open pull request), a normal push is rejected because you rewrote history. Then a force push is expected. Use --force-with-lease, not --force. It only overwrites the remote branch if it is still where you last saw it.

# Replace the remote branch only if nobody pushed to it since your last fetch
git push --force-with-lease origin BRANCH
# Real output from the test repository, as run (the exit code line `rc=1` is left out): Bob pushed again after Alice's last fetch, so the lease refused
$ git push --force-with-lease origin main
 ! [rejected]        main -> main (stale info)
error: failed to push some refs to '/tmp/gp/origin.git'

(stale info) is the lease doing its job: someone else's commit is on the remote. Fetch, rebase onto it (section 3), and only then push with the lease again. A plain git fetch updates what the lease compares against, and a background fetch (some editors do this) does the same. In our run, after git fetch origin without integrating Bob's commit, the lease let the push through and Bob's commit was gone from the remote branch:

# Real output from the test repository, as run: fetched, not integrated, then the lease push
$ git fetch origin
   5843450..74f0a39  feature    -> origin/feature
$ git push --force-with-lease origin feature
To /tmp/gp/origin.git
 + 74f0a39...74dd8f9 feature -> feature (forced update)

Adding --force-if-includes checks that you have actually integrated what you fetched. In the same state it refused (! [rejected] feature -> feature (remote ref updated since checkout)).

Never force-push main or another shared branch without agreement from the people who use it.

6. The server rejected the push

These come from the hosting service, not from Git itself. They were not reproduced on the test machine, and the exact wording depends on the host.

  • ! [remote rejected] main -> main (protected branch hook declined), or a GitHub message mentioning a protected branch or repository rules: the branch only accepts changes through a pull request, signed commits, or passing checks. Push to a new branch and open a pull request.
  • (pre-receive hook declined) with lines starting with remote: above it: a server hook refused the push. The remote: lines say why, for example a commit message format or a secret scanner.
  • Permission denied or 403: you can read the repository but not write to it. That is an access problem, not a history problem.

7. Verify

# Your branch and the remote branch now point to the same commit
git fetch origin
git status -sb | head -n 1

The first line shows ## main...origin/main with no ahead or behind. In the hosting service, the colleague's commits and yours are both in the branch history.

8. Rollback

  • Undo a rebase you just finished: git reset --hard ORIG_HEAD (Git saves the pre-rebase position there). This discards uncommitted changes, so commit or stash them first.
  • Undo an unpushed merge from git pull: git reset --hard ORIG_HEAD as well. We ran this on a real merge commit (git pull --no-rebase --no-edit origin main; --no-edit only skips the editor). ORIG_HEAD was the pre-merge tip, and the reset brought back the pre-merge history.
  • Find any earlier state of your branch: git reflog lists every position HEAD had, so commits that look lost after a rebase or reset can be recovered with git branch rescue HASH. In our run the rescued commit contained its file (lost.txt | 1 +).
  • How long a lost commit can be recovered: in our tests (Git 2.53.0, a commit on a branch that was then deleted with git branch -D) the commit was gone for good only after both of these ran: git reflog expire --expire=now --all and then git gc --prune=now. Afterwards git cat-file -t HASH printed fatal: git cat-file: could not get object info (exit code 128), git fsck --lost-found printed nothing, and git branch rescue3 HASH failed with fatal: not a valid branch point. Other combinations we ran:
    • git reflog expire --expire=now --all alone: the commit stayed. git fsck --lost-found printed dangling commit df00ce74de61eb69ce08e445d5946355e5fbd62b and git branch rescue2 HASH rescued it.
    • Plain git reflog expire (no flags) followed by git gc --prune=now: the commit stayed (dangling). In our run the plain command removed nothing.
    • git gc --prune=now alone: the commit stayed, because the reflog still referenced it.
    • git reflog expire --expire=now --all followed by plain git gc: the commit stayed (we did not test why; the Git default for pruning is two weeks).
    • git reflog expire --expire-unreachable=now --all followed by git gc --prune=now, and git reflog expire --expire=now --all followed by git prune --expire=now: both removed it.
  • You force-pushed over someone's work: the other person's commits can still be in their own clone, so ask them to push their branch again. We did not test the host's activity log.

Warning: restoring a commit with a force push overwrites whatever is on the remote branch now. The command is git push origin HASH:refs/heads/main --force-with-lease. It only works after git fetch origin: without the fetch, our run was rejected with (stale info). After the fetch it succeeded (forced update), and the other person's newer commit (Bob force commit) was no longer on main. Agree on this with the people who use the branch first.

# Real output from the test repository, as run (the exit code lines and the log lines between the commands are left out): the same restore push before and after git fetch origin
$ git push origin 4e25c3527f921215826026c1af3483c9111b386f:refs/heads/main --force-with-lease
To /tmp/gp/origin.git
 ! [rejected]        4e25c3527f921215826026c1af3483c9111b386f -> main (stale info)
error: failed to push some refs to '/tmp/gp/origin.git'
$ git fetch origin
From /tmp/gp/origin
 + 4e25c35...b7e5384 main       -> origin/main  (forced update)
$ git push origin 4e25c3527f921215826026c1af3483c9111b386f:refs/heads/main --force-with-lease
To /tmp/gp/origin.git
 + b7e5384...4e25c35 4e25c3527f921215826026c1af3483c9111b386f -> main (forced update)

What we have not verified

  • Section 6 (server rejections: protected branch, hook, 403): a local bare repository cannot produce them.
  • Behavior on a real hosting service, including its activity log and force-push rules on protected branches. The commands above ran against a local bare repository only.
  • Other Git versions (only Git 2.53.0 was used), and the gc.pruneExpire and gc.reflogExpire settings and packed objects: the lost-commit tests used loose objects and default configuration.
  • Restoring a commit that a colleague force-pushed over when it is no longer in your own clone: not run.
  • All commands ran on an Ubuntu 26.04 test machine. None of the commands here are distribution-specific, but we did not run them on RHEL, AlmaLinux or Rocky.

References

Official reference

git-scm.com ↗

Technical steps are original explanations; consult the upstream documentation for version-specific details.

Behavior differs by distribution and tool version. Commands are examples for authorized systems; review output for secrets before sharing.

Found this useful?

Follow us on YouTube for video explainers, and find us on Facebook to keep up with what is new.