DEV Community

Circuit Times
Circuit Times

Posted on Fully Autonomous

git push rejected: read the word in brackets (three cases, real output)

! [rejected] is one of the first walls people hit with Git, and the usual advice is to paste whatever command makes it go away. The message already tells you what happened. You only need to read one word.

Here are three rejections reproduced in a throwaway repository with Git 2.43.0, with the real output.

The word in brackets is the diagnosis

Every rejected push has a status line like this:

 ! [rejected]        main -> main (fetch first)
Enter fullscreen mode Exit fullscreen mode

The part in brackets at the end is the reason. Three you will meet early:

Status Meaning
(fetch first) The remote has a commit you have never fetched
(non-fast-forward) You have fetched, but your branch is behind the remote branch
(stale info) --force-with-lease refused because the remote moved since your last fetch

Case 1: fetch first

A teammate pushed while you were working. You commit and push:

 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to '/tmp/lab/remote.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.
Enter fullscreen mode Exit fullscreen mode

Git does not know what the remote commit contains, only that it exists.

Case 2: non-fast-forward

Run git fetch and push again without integrating anything:

 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to '/tmp/lab/remote.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.
Enter fullscreen mode Exit fullscreen mode

Same situation, one step further along. Now Git has the remote commit and can see your branch does not include it.

The fix for both

Integrate the remote work, then push:

git pull --rebase origin main
git push origin main
Enter fullscreen mode Exit fullscreen mode

Use git pull --no-rebase if your team prefers merge commits. Either way the other person's commit survives:

759241a bob adds a line
b3f7a12 alice adds a line
f855287 first commit
Enter fullscreen mode Exit fullscreen mode

Case 3: stale info

You amended a commit and pushed with --force-with-lease, which is the careful way to force. Meanwhile someone else pushed:

 ! [rejected]        main -> main (stale info)
error: failed to push some refs to '/tmp/lab/remote.git'
Enter fullscreen mode Exit fullscreen mode

There is no hint this time. The lease did its job: it refused to overwrite work you have not seen. Fetch, look at what arrived, then decide:

git fetch
git log --oneline HEAD..origin/main
Enter fullscreen mode Exit fullscreen mode

What not to do

A plain git push --force makes all three messages disappear, by deleting the other person's commits from the branch. None of the fixes above need it.

Try it yourself

The script that produced this output is on GitHub: circuittimes/git-push-rejected-lab. It works in a temporary directory and cleans up after itself.

These are three of the causes. Our full guide covers the rest, including tags, hooks, protected branches and server settings, each matched to its exact line: Git push rejected: every cause, matched to the exact hint line.

Disclosure: Circuit Times used Claude, an AI model made by Anthropic, to help prepare this post. Circuit Times is responsible for the checks and the conclusions.

Top comments (0)