Read the line above the error
Git ends several kinds of rejected pushes with failed to push some refs. Look for the reason in parentheses on the line for your branch. With [rejected] (fetch first) or [rejected] (non-fast-forward), the remote branch points to a commit your push would leave out of its history. Fetch that branch and bring its commits into yours before you try again.
If Git reports a protected branch, a hook rejection or a permissions error, follow that message first. The steps below are for a branch whose history has moved.
Check where the push went
In acme/web, the local branch is cart-fix and the push to cart on origin was rejected. Before you fetch, check that origin reads from the repository that rejected the push.
git remote get-url origin
git remote get-url --push --all origin
The first command shows the fetch URL. The second lists the push URLs. If both commands point to the same repository and there’s only one push URL, use origin below.
If origin fetches from a different repository or lists several push URLs, give the rejected destination its own remote. Match the To line in Git’s output to a push URL and add that URL as publish.
git remote add publish \
https://git.example.com/acme/web.git
git remote get-url publish
git remote get-url --push --all publish
Confirm that publish fetches from that repository and has a single push URL for it. Then use publish in place of origin in both the fetch and the retry below.
Fetch and compare the two branches
Check that you’re on the local branch you meant to push. Fetch the exact destination branch from the remote you just checked.
git status -sb
git fetch --no-prune --no-prune-tags \
origin refs/heads/cart
A successful fetch records the tip of cart in FETCH_HEAD, even if your clone only tracks other branches. If the fetch fails, stop and address its error before continuing.
git log --left-right --format='%m %s' \
FETCH_HEAD...HEAD
> Fix cart note
< Rename cart heading
< Add coupon copy
Another fetch can replace FETCH_HEAD. If one runs before you rebase or merge, fetch refs/heads/cart from the same remote and compare again. If Git reports an unexpected forced update, check with your team before continuing.
In the log, < marks commits only on the fetched branch and > marks commits only on your current branch.
Both markers mean both sides have work to keep. Only < means you’re behind with nothing new to push. With a clean working tree, bring your local branch up to date with git merge --ff-only FETCH_HEAD. If you expected local commits, check which branches have unpushed commits.
If git status also says your branch and its upstream have diverged, see how to read and resolve a diverged branch.
Rebase or merge the remote changes
Start with a clean working tree. Commit or stash any edits you want to keep.
If the local commits are yours alone and your team uses a linear history, rebase to replay them on top of the fetched branch. This gives the replayed commits new IDs.
git rebase FETCH_HEAD
If your team allows merge commits and you want to keep the existing commit IDs, merge instead.
git merge --no-edit FETCH_HEAD
If Git stops at a conflict, edit and stage the resolved files. Run git rebase --continue or git merge --continue, matching the operation you started. For a rebase, repeat for each conflict Git reports. To return to where you started, use git rebase --abort or git merge --abort while the operation is still in progress.
Push again
Once the rebase or merge is complete, review the result and run your usual checks. Push the checked-out branch to cart using the same remote you fetched from. If you fetched from publish, use publish here too.
git push origin HEAD:refs/heads/cart
HEAD selects the commit you just checked. git push origin cart would select the local branch named cart, even while cart-fix is checked out.
If the remote branch moved again while you were working, fetch and check it again. A normal push keeps the remote’s commits in the branch history. A force push can remove them.
Keep unpushed commits in view
push& (Pushand) is a Mac menu bar app that shows which of your repositories and worktrees have commits waiting to be pushed. See how it works.