How to fix issue in Git: "Updates were rejected because a pushed branch tip is behind its remote counterpart"

user3789553 picture user3789553 · Sep 8, 2014 · Viewed 20.7k times · Source

I'm trying to push commits to a remote and getting this error message:

$  git push origin master
To [email protected]:hbrosuru.git
! [rejected]        ab68c0485d -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:hbrosuru.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I think I've got in a muddle somewhere by pushing multiple local branches to this single remote branch and I'd like to basically clear out what's on the server and push a whole branch from fresh. Is this possible, or is there a better way to fix this error?

Ps. this remote branch is hosted on Fortrabbit so I've not got full access to the server to simply delete the branch and create a new one because Fortrabbit's deployment mechanism is based on Git.

Answer

Chris picture Chris · Sep 8, 2014

I'd like to basically clear out what's on the server and push a whole branch from fresh.

This likely means a force push. Note that force pushing is generally not a safe thing to do, and should especially be avoided when pushing to a shared source code repository like GitHub. But in this situation, where you are pushing to update a PaaS server, it is likely okay.

You might want to visualize your branches graphically using gitk, git log --all --graph --decorate, or some other tool before force pushing, just to make sure that everything looks as you expect.

To force push your local master branch to your origin remote's master branch, run

git push --force-with-lease origin master:master

The --force-with-lease argument will cause the force push to succeed only if your local branch is up to date with respect to the one you're pushing to. (This prevents accidental overwriting of commits you don't know about.) I strongly advise using the with-lease variant whenever possible instead of the old --force argument, which is less safe.