John Little
John Little

Reputation: 12447

Cannot push to BitBucket - fatal: refusing to merge unrelated histories

I created a new Bitbucket repo, and by default it created it with a .gitignore.

I have a large project not yet in source control.

This doc says to do the following:

git init
git add --all
git commit -m "Initial Commit"
git remote add origin https://[email protected]:7999/yourproject/repo.git 
git push -u origin master

The push failed and said needed to pull.

git pull gave this error:

There is no tracking information for the current branch.

So I googled it and did this:

git branch --set-upstream-to=origin/master master

Now I can do git pull, but I get this error:

fatal: refusing to merge unrelated histories

This:

 git push --force origin master

Didn't work as we are not allowed to push directly to master or develop, I tried this:

git checkout -b feature/1-initial
git push origin feature/1-initial

Then Bitbucket lets me create a PR, but says "Something went wrong Unrelated branches" no matter if I select develop or master.

Maybe I should start again like this:

  1. Delete .git dirs from current project (will this work?)
  2. Clone the Bitbucket project in a new directory.
  3. Check out develop
  4. Create a branch
  5. Bulk copy my project into this directory.
  6. Push?

I haven't heard of anyone doing this, but it seems the only tenable solution.

The big downside of this approach is I will lose all my IntelliJ config, including run profiles etc., as I will need to import a new project from scratch in the new dir.

Upvotes: -3

Views: 74

Answers (2)

John Little
John Little

Reputation: 12447

The solution is this:

  1. don't bother doing git init in your current project dir.
  2. clone the new Bitbucket repo in a new directory.
  3. cd to the new dir
  4. git checkout develop
  5. git checkout -b feature/1-initial
  6. make a backup of your work.
  7. recursively remove any .git, .idea, .mvn directories by hand or with a Bash script
  8. dump your cleaned code and directories into the new dir.
  9. git add, commit and push to origin
  10. crate a new PR with your branch and merge to develop.
  11. recreate the lost IntelliJ run profiles and similar IDE config.

The instructions on the Bitbucket site are woeful.

Upvotes: -1

dani-vta
dani-vta

Reputation: 7130

When you created a repo on Bitbucket with a .gitignore, Bitbucket created the repo for you with an initial commit to record the addition of the .gitignore file.

This commit is only present in the history of your remote repository, hence it is unrelated to the history of your local repository. Since there is really not that much history going on on your remote, you could force push to overwrite the history of the remote's master branch with your local master branch.

git push --force origin master

However, in case the .gitignore was defined only on the remote and you're interested in preserving it, make sure to download it or copy its content before proceeding with the push.


Alternatively, if you have protection rules in place (and as you've already mentioned after your edit), you can:

  1. Clone your remote repository.
  2. Create a new temporary branch temp from master.
  3. Copy/overwrite the content of the cloned repo with your local repo.
  4. Commit the change.
  5. Push temp to the remote repository.
  6. Create a Pull/merge request from temp to master.

Upvotes: 1

Related Questions