Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 892
user3789553 Avatar asked Sep 08 '14 16:09

user3789553


People also ask

Why is Github rejecting my push?

Sometimes, Git can't make your change to a remote repository without losing commits. When this happens, your push is refused. If another person has pushed to the same branch as you, Git won't be able to push your changes: $ git push origin main > To https://github.com/USERNAME/REPOSITORY.git > !

What to do if git push is not working?

If git push origin master not working , all you need to do is edit that file with your favourite editor and change the URL = setting to your new location. Assuming the new repository is correctly set up and you have your URL right, you'll easily be able to push and pull to and from your new remote location.

How do I force a branch to push on github?

To force a push to only one branch, use a + in front of the refspec to push (e.g git push origin +master to force a push to the master branch). See the <refspec>... section above for details. Force an update only if the tip of the remote-tracking ref has been integrated locally.

Does git push force affect other branches?

It's well known that git's push --force is strongly discouraged as it can destroy other commits already pushed to a shared repository. This isn't always completely fatal (if the changes are in someone's working tree then they can be merged), but at the very least it's inconsiderate, at worst disastrous.


1 Answers

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.

like image 173
Chris Avatar answered Oct 13 '22 02:10

Chris