BitBucket - error: failed to push some refs
Asked Answered
T

11

15

I have some projects I would like to upload on my bitbucket private profile. I performed the following steps but I get an error.

  1. Convert my project to git with: git init

  2. Next:

git add
git commit -m "some message"
  1. I created a bitbucket repository and version control system is GIT.

  2. I then type this (of course with real account name and reponame and repo owner):

git remote add origin https://<repo_owner>@bitbucket.org/<accountname>/<reponame>.git
  1. Finally,
git push -u origin master

I did all of this and my terminal gives me this error:

To https://bitbucket.org/milosdev_me/lfs.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://[email protected]/milosdev_me/lfs.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first 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.
Thaine answered 26/8, 2019 at 21:12 Comment(2)
Possible duplicate of git error: failed to push some refs toAncel
Fix some formatting and improve English.Taal
M
47

Try:

git push origin master --force

This works for me.

Misology answered 8/9, 2020 at 1:29 Comment(3)
this worked fine. But why this error occurs?Sedimentary
@CB Shrestha: Error occurred mostly due to difference between local repo and bitbucket repo. In my case, bitbucket repo has 1 file which is not present in my local repoReganregard
@CBShrestha - it can also occur if you have a local repo and are attempting to push it to different remote name. ie, say you have a local branch my_cool_branch and you do a git push origin my_cool_branch_oops_typo. You can force it but be double check both the local and remote branch names to verify it's what you want.Farra
F
7

Your master branch has some code that you don't locally have, and to prevent you from conflicts, it doesn't let you to push further changes, before you have them locally. To resolve this, pull all the changes to your local repository (your project):

git pull origin master --allow-unrelated-histories

After that, you will have all the code that is available on your master branch.

NOTE: Be careful, pulling the code from remote branch might mess up all the changes locally. Make sure to save those changes somewhere, or create another branch locally where you will pull your origin master branch, so it doesn't mess up your changes.

Freeloader answered 26/8, 2019 at 21:39 Comment(4)
Okay I did it but it returns me From https://bitbucket.org/milosdev_me/bpwebsite * branch master -> FETCH_HEAD fatal: refusing to merge unrelated historiesThaine
Still nothing... Omg I didn't know it's so difficultThaine
A lot of them and I quit. I can't take this anymore, I'm so nervous about it. Thank you for your help and your time!Thaine
Just take it easy, often, such problems have a pretty stupid and simple solutions that you will laugh about in the future. One of the solutions could be to make a new repository and new project from scratch, and then try to push that empty project first.Freeloader
T
4

Adding --force option is a bad idea
Either rebase it or pull the code, merge it and push it.

git pull --rebase origin master
git push -u origin master
Tapp answered 19/3, 2021 at 11:8 Comment(0)
P
2

Your issue is that you have different things between your local repository and your git repository (probably a readme file that you created automatically), so you have two options:

  1. use git pull origin master, with this command, you will pull from your git repository, but it will cause a merge conflict, which you have to resolve using an IDE (recommended to beginners) or through cmd.

  2. use git push origin master --force, this way, you will force your push from your local repository to your git repository, ignoring any merge conflict by overwriting data. I'm not sure, but I think it will overwrite all git repository data with you local repository data (which is what you want).

Perrine answered 25/8, 2020 at 12:58 Comment(0)
N
1

Your remote repository and local repository have differences, something new in remote repository. So for pushing you need pull changes, from remote, previously. Try do:

git pull
git push -u origin master
Negligent answered 26/8, 2019 at 21:18 Comment(9)
It returns me this: error: failed to push some refs to 'https://[email protected]/milosdev_me/bpwebsite.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.Thaine
Okay. What git pull returns?Negligent
There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details. git pull <remote> <branch> If you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=origin/<branch> master it returns thisThaine
Okay so try this: <code> git branch --set-upstream-to=origin/master master git pull git pushNegligent
So this first git branch --set-upstream-to=origin/master master ? Than git pull and then git push?Thaine
No problem for formatting. I did it but after git pull it returns me fatal: refusing to merge unrelated histories And after git branch --set-upstream-to=origin/master master it returns me this: Branch 'master' set up to track remote branch 'master' from 'origin'.Thaine
Okay, what about not beautiful solution, create new directory, in new directory make git clone https://<repo_owner>@bitbucket.org/<accountname>/<reponame>.git after this copy all files from old directory to new, and make git add . && git commit -m "some message" , and after you can make git pushNegligent
How you mean new directory? Repository or something else?Thaine
No, simple directory. For example you local repository in /home/username/my-project/ , and you need create new directory for example mkdir /home/username/my-project-git/ after this, you need go to new directory, and make git clone https://<repo_owner>@bitbucket.org/<accountname>/<reponame>.git copy all files from /home/username/my-project/ via cp -r /home/username/my-project/* /home/username/my-project-git/ and after make git add . && git commit -m "some message" and git pushNegligent
S
1

The issue is because your remote repository and local repository have differences.I had same issue and i tried all the above mentioned solution but nothing worked for me. For me the scenario was :- I already had my branch in remote repository.If you have the same scenario then follow below steps:-

  1. run command 'git pull'

  2. delete branch from local repository

  3. checkout that particular branch using "checkout as a new local branch" from the Remote repository.

  4. run command 'git branch -u <your_branch_name>'

  5. run command 'git push or git push --force'

or you can try directly from step 4 first , if it does not work then follow entire steps.Hopefully it will help someone.

Sproul answered 6/8, 2020 at 12:36 Comment(0)
Y
1

If you have your bitbucket account linked to jira.

(this answer will work for you, only if you have your jira account linked to bitbucket)

I was having the same problem trying to push my current branch with the origin.

for example: my branch name was:

feature/PREFIX-000-new-name-branch.

previous commit:

git commit -m "Write your commit here"

so far it was not working.

you have to mentioned the ticket name in the commit. if you have made the commit, make an --amend to rename your commit and retry it.

git commit  --amend -m "PREFIX-000 Write your commit here"

try the push again.

Yellowish answered 7/3, 2022 at 2:43 Comment(1)
This is the one and only solution from my view and should be the accepted answer. Thank you Antonio!Sect
K
1

If you are using BitBucket, this issue occured when I tried to push to a branch but that branch has writing disabled via the repository settings.

Kleenex answered 8/3, 2022 at 13:41 Comment(0)
B
0

Check the name of your local branch as by default git might have named it 'main'.

Bitbucket uses 'master', so rename your local repo branch from 'main' to 'master'.

And then run:

git push -u origin master
Beka answered 22/8, 2023 at 19:0 Comment(0)
T
-2

if you have already created a project locally and you want to upload it to git, you will then need to do:

  1. git status to see the changes you need to upload
  2. git add . to add those changes to your repo
  3. git commit -m "" to add a commit message
  4. git push origin master

that way I solved the very same problem I was having.

Tesstessa answered 29/7, 2020 at 3:36 Comment(0)
M
-2

it might be a configuration issue

I fixed this issue after updating the global user.email value with my email

git config --global user.email [email protected]

Note: You need to delete the previous commit because it had the wrong email in the commit

Microbicide answered 8/2, 2021 at 10:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.