14

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://milosdev_me@bitbucket.org/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.
rlandster
  • 7,294
  • 14
  • 58
  • 96
Milos
  • 552
  • 2
  • 7
  • 32

11 Answers11

45

Try:

git push origin master --force

This works for me.

Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
Alfredo Morales
  • 950
  • 6
  • 6
  • 2
    this worked fine. But why this error occurs? – cbisum Mar 26 '21 at 05:35
  • @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 repo – nambk Jul 23 '21 at 07:59
  • @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. – Noel Oct 25 '21 at 14:49
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.

zlatan
  • 3,346
  • 2
  • 17
  • 35
  • Okay I did it but it returns me ```From https://bitbucket.org/milosdev_me/bpwebsite * branch master -> FETCH_HEAD fatal: refusing to merge unrelated histories``` – Milos Aug 26 '19 at 21:44
  • Still nothing... Omg I didn't know it's so difficult – Milos Aug 26 '19 at 21:52
  • 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! – Milos Aug 26 '19 at 21:56
  • 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. – zlatan Aug 27 '19 at 07:40
3

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
sheel
  • 467
  • 8
  • 23
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).

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
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
Klim Mihno
  • 11
  • 1
  • It returns me this: ```error: failed to push some refs to 'https://milosdev_me@bitbucket.org/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.``` – Milos Aug 26 '19 at 21:26
  • Okay. What ```git pull``` returns? – Klim Mihno Aug 26 '19 at 21:31
  • 1
    ```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 If you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=origin/ master``` it returns this – Milos Aug 26 '19 at 21:33
  • Okay so try this: git branch --set-upstream-to=origin/master master git pull git push – Klim Mihno Aug 26 '19 at 21:36
  • So this first ```git branch --set-upstream-to=origin/master master``` ? Than ```git pull``` and then ```git push```? – Milos Aug 26 '19 at 21:38
  • 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'.``` – Milos Aug 26 '19 at 21:42
  • Okay, what about not beautiful solution, create new directory, in new directory make `git clone https://@bitbucket.org//.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 push` – Klim Mihno Aug 26 '19 at 21:48
  • How you mean new directory? Repository or something else? – Milos Aug 26 '19 at 21:52
  • 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://@bitbucket.org//.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 push` – Klim Mihno Aug 26 '19 at 21:56
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.

soni kumari
  • 313
  • 4
  • 4
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.

  • This is the one and only solution from my view and should be the accepted answer. Thank you Antonio! – Mike_H Jan 10 '23 at 08:20
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.

Shane
  • 659
  • 1
  • 9
  • 19
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
iAmcR
  • 859
  • 11
  • 10
-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.

Hamid Ali
  • 875
  • 1
  • 8
  • 22
angra84
  • 11
-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 my@email.com

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

yonadav bar ilan
  • 550
  • 6
  • 10