2

On the official Bitbucket website we can read:

Locally, change to the root directory of your existing source.
Initialize the project by running the following commands in the terminal:

git init
git add --all git commit -m "Initial Commit"
Log into Bitbucket Server and create a new repository.
Locate the clone URL in the nav panel on the left (for example: https://username@your.bitbucket.domain:7999 /yourproject/repo.git).
Push your files to the repository by running the following commands in the terminal (change the URL accordingly):

git remote add origin https://username@your.bitbucket.domain:7999/yourproject/repo.git
git push -u origin master Done! Your repository is now available in Bitbucket Server.

Source: https://confluence.atlassian.com/bitbucketserver/importing-code-from-an-existing-project-776640909.html

and after this commands I got:

To https://bitbucket.org/myrepo/myapp.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://myrepo@bitbucket.org/myrepo/myapp.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.

so now I'm try fetch

git fetch origin

and again git push -u origin master

and I got

> To https://bitbucket.org/myrepo/myapp.git 
> ! [rejected]        master -> master (non-fast-forward) error: failed
> to push some refs to 'https://myrepo@bitbucket.org/myrepo/myapp.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.

Somebody can help me ? I waste too many days for this git

purcha
  • 371
  • 3
  • 12
  • The issue relates to the remote repository having changes on it that are not in your local repository. This may be because the remote repository was created independently of your local version. There are several ways to approach this so take a look at the answers here https://stackoverflow.com/questions/10298291/cannot-push-to-github-keeps-saying-need-merge – karen Sep 26 '18 at 12:45
  • Bitbucket isn't lying, that's the documentation for a new repo, not one which already has a history. – evolutionxbox Sep 26 '18 at 13:04

4 Answers4

3

The error message you are getting is because the repository already has existing commits. If you are using bitbucket.org (cloud bitbucket) rather than Bitbucket server, it offers to create a read me for you. This puts a commit in the repo that will prevent the steps you reference from working. Those steps work if the repo is empty when you perform them

Rich Duncan
  • 1,845
  • 1
  • 12
  • 13
2

git fetch origin is not enough, you have to run git merge origin/master before being able to push.

Romain Warnan
  • 1,022
  • 1
  • 7
  • 13
2

After latest updates on Bitbucket, newly created remote and local repositories will not have same base. So you have to pull with following command

git pull origin master --allow-unrelated-histories 

Then you can push your local branch to remote.

Zaid Mirza
  • 3,540
  • 2
  • 24
  • 40
1

You can always use git push --force to force set remote master to your local one.

Probably you have some file that not exist on your local repository.

With git push --force you will override changes. Be careful in other situations, but here it can works, because you don't want to have anything on master except your local changes.

Of course you can try with git pull and after that push once again it should solve problem.

But it's only answer on problem, not on question about bitbucket documentation.

Patryk Rudnicki
  • 755
  • 1
  • 8
  • 21