1

I keep getting the following error when I try to push to Bitbucket. I used to work just fine and it just stop working.

Can someone please help me understand what does this mean and how to solve it?

Pushing to https://x-token-auth@bitbucket.org/UserName/project-name.git
To https://bitbucket.org/UserName/project-name.git
 = [up to date]      Redesign -> Redesign
 = [up to date]      Version4.0 -> Version4.0
 = [up to date]      version2.0 -> version2.0
 = [up to date]      version3.0 -> version3.0
 = [up to date]      version3.2 -> version3.2
 = [up to date]      version4.1 -> version4.1
 = [up to date]      version5.0.3+cocoapods-firebase -> version5.0.3+cocoapods-firebase
 = [up to date]      version5.1 -> version5.1
 = [up to date]      version5.2 -> version5.2
 = [up to date]      version5.3 -> version5.3
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://x-token-auth@bitbucket.org/UserName/project-name.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

FYI - I'm not sure how to use git pull as suggested in the error. Also, I'm using git-tower as my git GUI.

fs_tigre
  • 10,650
  • 13
  • 73
  • 146
  • 2
    `Updates were rejected because the tip of your current branch is behind hint: its remote counterpart.` ... follow the advice contained in the error message and do a `git pull` (or maybe a `git pull --rebase`). – Tim Biegeleisen Jul 22 '19 at 12:45
  • 1
    Additionally `git merge origin/mater` will work too since the refs have been updated – EncryptedWatermelon Jul 22 '19 at 12:47
  • 1
    `pull` as in the message is basically `fetch`+`merge`. Git prevents you from pushing onto a remote that holds a newer version of a branch than your local. This would be against all versioning how-tos... – harmonica141 Jul 22 '19 at 12:48
  • @TimBiegeleisen - Is it safe to run `git pull`? Will this override my local changes with data from `Bitbucket`? I do not want to override my local data since it's a lot newer than what's in `Bitbucket`. – fs_tigre Jul 22 '19 at 12:50
  • 1
    One of two things would happen when you try to `git pull`. If your local changes have nothing to do with the changes in the upstream, then the pull would succeed, leaving your working directory and stage basically the same. If Git _can't_ do the pull, then you would likely see an error message telling you to commit or stash your local work, because the pull would overwrite that work. So, in that case, you would have to first stash or commit, then pull. – Tim Biegeleisen Jul 22 '19 at 12:54
  • If I understand you correctly it seem to be safe to run it since my `Staging Area` is cleaned and I have committed all local changes, correct? – fs_tigre Jul 22 '19 at 13:01
  • 1
    Yes, you should `pull`. Let me write that as a formal answer for you. – harmonica141 Jul 22 '19 at 13:47
  • Possible duplicate of [Cannot push to GitHub - keeps saying need merge](https://stackoverflow.com/questions/10298291/cannot-push-to-github-keeps-saying-need-merge) – phd Jul 22 '19 at 18:17
  • https://stackoverflow.com/search?q=%5Bgit%5D+hint%3A+Updates+were+rejected+because+the+tip+of+your+current+branch+is+behind – phd Jul 22 '19 at 18:17

2 Answers2

2

Why: The error occurs due to the fact that remote holds a newer version of master than local. In this case git forbids to push to the branch because - what will be merged and how and who resolves the conflicts? Someone always has to be responsible, the repository can not do this job on its own.

Git forces you to pull first - pull being equivalent to fetch+merge - so you collect the newest version of the code to your local and by doing so inherit the responsibility to merge in your code and resolve the merge conflicts that you created by writing your code. Otherwise it would be possible to just offload them to remote and leave a mess behind with no owner.

What to do: git pull. You will receive a message asking you to do a merge. In case of a merge conflict you will be asked to resolve it with any tool of your choice (git tower in your case I suppose).

After that fast forward pushes will be possible again until someone pushes to master again and the branch again holds a newer version of the common code base than your local.

How to get around it:...(partly)... On Bitbucket and Github you can deploy restrictions to push to certain branches. This is called 'protecting branches'. By doing so you can forbid everyone to push to the protected branches and proceed on a white-list basis. You can define restrictions for pushing, e.g. no merge commits on this branch, mandatory pull requests before merging etc. This will stop people from randomly pushing to production branches and rule out major sources for issues.

harmonica141
  • 1,389
  • 2
  • 23
  • 27
  • Thank you for the explanation. The interesting thing here is the that this is a private repository in which I'm the only person working on it from a single computer. In other words, I'm not sure how the remote branch could be newer since no one else is making changes. – fs_tigre Jul 22 '19 at 13:58
  • 1
    Maybe once you pushed to the wrong branch? You could find out using the graphical history. – harmonica141 Jul 22 '19 at 14:02
  • I only have the master branch. – fs_tigre Jul 22 '19 at 14:24
  • Weirdly, the pull request in this case gave the "fatal: refusing to merge unrelated histories" error. The other answer made it work though. – Vörös Imi Jan 18 '21 at 23:58
  • Hi, I am having the same issue. I am the only ome working on my remote repository. Everything works find and suddently I cannot push anymore. `git push origin master` returns `fatal: the remote end hung up unexpectedly fatal: the remote end hung up unexpectedly error: failed to push some refs to...` `git pull` says **Already up to date.** Any idea/reason why getting this error suddently while I am the only one working on my repository? Thanks – bkm Mar 05 '21 at 06:21
  • @bkm This is a different problem. Please open a new question. – harmonica141 Mar 05 '21 at 09:25
  • Thanks @harmonica141 I posted the new question [here](https://stackoverflow.com/questions/66498706/failed-to-push-to-bitbucket-repository) – bkm Mar 05 '21 at 19:43
1

You may try git push -u origin master--force

Sarath P
  • 21
  • 1
  • Couldn't figure out how to push the project to a brand new bitbucket repo that I've made, probably because of a "demo" .gitignore file that I deleted online. THIS solved it! Although I still don't know what the "correct" method would be, but IT WORKS! Thank you for sharing this answer. – Vörös Imi Jan 18 '21 at 23:55