PricingSign in52Download

Blog / git

Push rejected after your coding agent amended a commit

Your agent amended or rebased a pushed commit and git push says non-fast-forward. Check what differs, then push with a lease, not git pull or a plain --force.

On this page
  1. What the rejection says
  2. Why git pull is the wrong fix here
  3. Check what's actually different
  4. Push with a lease, not --force
  5. The fetch loophole
  6. Keep the teammate's commit, then push
  7. Tell whoever else has the branch
  8. Stop it happening again
  9. Where Joxo fits
  10. Frequently asked questions
  11. Why is my push rejected as non-fast-forward after an amend?
  12. Should I run git pull after a non-fast-forward rejection?
  13. What is the difference between --force and --force-with-lease?
  14. What does --force-if-includes do?
  15. How do I undo a git pull that merged the old commit back?
  16. Try it with your team

Your agent pushed a branch, then tidied it: it amended the last commit to fix a typo, or rebased onto main, or squashed its commits. Now git push fails with "non-fast-forward", and the agent is about to run git pull or git push --force to make the error go away. Both can do damage. Here's what the error means, how to check what's really different, and the safe push. We ran every command with git 2.43 against a local remote.

What the rejection says#

bash
git push
 ! [rejected]        feature -> feature (non-fast-forward)
error: failed to push some refs to 'github.com:you/app.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.

The hint assumes someone else pushed and you need their work. After an amend or a rebase, that's usually not the case: your branch has a new version of commits the remote already has under different hashes. Git can't tell the difference, so it refuses. git status shows the split:

## feature...origin/feature [ahead 1, behind 1]

Ahead 1 is the agent's amended commit. Behind 1 is the original it replaced.

Why git pull is the wrong fix here#

Following the hint makes things worse. With no pull setting configured, git 2.43 stops and asks how to combine them:

hint: You have divergent branches and need to specify how to reconcile them.
fatal: Need to specify how to reconcile divergent branches.

Agents then try git pull --no-rebase, which merges the old commit back in next to its new version. When both versions touched the same lines, that's a conflict in code you wrote once:

Auto-merging b
CONFLICT (add/add): Merge conflict in b
Automatic merge failed; fix conflicts and then commit the result.

And if it merges without a conflict, the branch now carries both copies of the commit plus a merge, which is the history the agent was trying to clean up. If that's where you are, git merge --abort gets you out of a merge that stopped on a conflict. If the merge already committed, git reset --hard ORIG_HEAD takes the branch back to just before it (check git status first, --hard drops uncommitted edits).

Check what's actually different#

Fetch, then compare your branch with the remote one:

bash
git fetch
git log --oneline --left-right HEAD...@{u}
< 2ce09d3 Add b, with fix
> 378b69a Add b

< lines are only on your side, > lines only on the remote. Here it's one commit replaced by its amended version, which is what you expect. To see the change between them:

bash
git diff @{u} HEAD

If that diff is the agent's tidy-up and nothing else, overwriting the remote is right. What you're looking for is a > commit you don't recognise, such as a teammate's commit or one their agent pushed. Overwriting would delete that from the remote.

Push with a lease, not --force#

bash
git push --force-with-lease
 + 378b69a...2ce09d3 feature -> feature (forced update)

A lease tells the remote: replace the branch only if it's still where I last saw it. A plain --force replaces it whatever is there. If someone pushed in the meantime, the lease refuses:

 ! [rejected]        feature -> feature (stale info)

That refusal is the point: it's the case where --force would have silently deleted their commit. See what to do when an agent force-pushed over your work if it already happened.

The fetch loophole#

"Where I last saw it" means your remote-tracking branch, origin/feature. A git fetch moves that forward, so after a fetch a plain lease passes even though you never looked at the new commit. Agents fetch constantly. Add --force-if-includes, which also checks that the remote's tip is in your own branch's history:

bash
git fetch
git push --force-with-lease --force-if-includes
 ! [rejected]        feature -> feature (remote ref updated since checkout)

Now the push stops even after a fetch, until the teammate's commit is part of your branch. You can make that the default with git config --global push.useForceIfIncludes true.

Keep the teammate's commit, then push#

If a teammate's commit is on the remote, put it on top of your rewritten branch:

bash
git log --oneline HEAD..@{u}
32cdc2f Teammate: add c
2ce09d3 Add b, with fix

The first is theirs, the second is the old version of your amended commit, which you leave out. Take only theirs:

bash
git cherry-pick 32cdc2f

Picking it changes its hash, so --force-if-includes still refuses: the remote's exact commit isn't in your branch. Name the commit you've now included instead, so the push only goes through if the remote is still at that commit:

bash
git push --force-with-lease=feature:32cdc2f
 + 32cdc2f...239f24e feature -> feature (forced update)

The teammate's change is kept, your amended commit replaces the old one, and if anyone pushed again in between, the push fails rather than losing it.

A git rebase @{u} looks like a shortcut but usually isn't: it replays your amended commit on top of the old version of itself, and stops on a conflict between the two.

Tell whoever else has the branch#

After any forced push, anyone who pulled the old branch has commits that no longer exist on the remote. If they pull, they hit the same divergence. If they have no work of their own on the branch, git fetch then git reset --hard @{u} brings them in line. If they do, they should take their own commits onto the new branch with git rebase --onto @{u} <old-tip>, where the old tip is the commit the remote used to point to.

Stop it happening again#

  • No rewriting after push. Tell your agent in CLAUDE.md or AGENTS.md: once a branch is pushed, add new commits; don't amend, rebase or squash it unless asked. Squash at merge time instead.
  • Never --force. If a rewrite is needed, the agent uses --force-with-lease --force-if-includes and stops on a refusal. In Claude Code, put git push --force and git push -f on the deny list in .claude/settings.json.
  • One agent per branch. Two agents pushing the same branch will reject each other's pushes all day. Give each its own branch; see stop agents editing the same files.
  • Protect main. On GitHub or GitLab, block forced pushes to main so a confused agent can't rewrite it.

Where Joxo fits#

The dangerous case is the one where a teammate's agent pushed to the branch your agent is about to overwrite. In Joxo, agents on one project see each other's work and can hand a branch over instead of racing on it, and you approve from the desktop app or your phone. Setup is one line pasted into your agent.

Frequently asked questions#

Why is my push rejected as non-fast-forward after an amend?#

Amending gives the commit a new hash. The remote still has the old one, so your branch and the remote have split, and git won't overwrite the remote without a forced push.

Should I run git pull after a non-fast-forward rejection?#

Not after you amended or rebased. Pulling merges the old version of the commit back in. Check with git log HEAD...@{u}, then push with --force-with-lease.

What is the difference between --force and --force-with-lease?#

--force replaces the remote branch whatever is on it. --force-with-lease only replaces it if it's still at the commit you last fetched, so a teammate's new push is not lost.

What does --force-if-includes do?#

It refuses a forced push unless the remote's current tip is in your local branch's history. That closes the gap where a git fetch makes a plain lease pass without you seeing the new commit.

How do I undo a git pull that merged the old commit back?#

If the merge stopped on a conflict, run git merge --abort. If it already committed, git reset --hard ORIG_HEAD takes the branch back to before the pull.