12

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

$  git push origin master
To git@git1.eu1.frbit.com:hbrosuru.git
! [rejected]        ab68c0485d -> master (non-fast-forward)
error: failed to push some refs to 'git@git1.eu1.frbit.com: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.

user3789553
  • 151
  • 1
  • 1
  • 7
  • Does `git push -f` work? That will forcibly overwrite the branch with whatever you are pushing now. But it's possible to disable it from the server, so you have to try and see. – Isaac Betesh Sep 08 '14 at 17:04
  • 1
    Also, what branch do you want to be on? Notice this line: `ab68c0485d -> master (non-fast-forward)` -- that means you are not on a branch, since your current HEAD is referred to by a commit hash (`ab68c0485d`) instead of a branch name (`master`). If you want the current HEAD to be the new master, use `git branch -f master ab68c0485d && git checkout master` and then try your push. – Isaac Betesh Sep 08 '14 at 17:06
  • Possible duplicate of http://stackoverflow.com/questions/10298291/cannot-push-to-github-keeps-saying-need-merge – Adil Apr 27 '15 at 08:28

1 Answers1

13

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.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • 3
    To reiterate for people who got here just from the general error, you probably **don't** want to force push unless you're in OP's specific situation. If you're sharing a repo with multiple other people and get this error, there's a decent chance you're legitimately behind and should resolve the conflict by first pulling the remote branch, resolving any merge conflicts, *then* pushing. – Sean Apr 29 '15 at 17:30
  • I had a similar problem. I am the only one using the given repository, and I ended up aborting a rebase and forcing a push (`-f`) and then I was back on track. – M. K. Hunter Mar 21 '17 at 13:58
  • Even if you do want to force push and get this command to work, there's a good chance your repo will tell you `remote: Force-pushing to this repository has been disabled`. So you might want to check that first. – Noumenon Dec 10 '19 at 16:58
  • "Note that force pushing is generally not a safe thing to do" Why? I'm the only person who has ever used the repo. – gargoylebident Oct 16 '20 at 16:51
  • @stackexchange_account1111, note the "generally". _In general_, this is not a safe thing to do. It's less unsafe with a single user, but still not entirely "safe" as you could (and in this case, in fact _will_) lose commits. Maybe that's what you want, and maybe it isn't. – ChrisGPT was on strike Oct 16 '20 at 19:52
  • I don't want it, but it seems like it's my only option, because git is bugged and refuses to merge the conflicts after pull (even when I do it manually). – gargoylebident Oct 18 '20 at 01:51