Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a pull request in Bitucket: error "Unrelated branches"

I'm having a small team working on a single repository, I asked my each teammates to create there own branch and work on there branch, all of them cloned the repository and then they did the following command:

git checkout -b BitPankaj

BitPankaj is illustration branch name, they started working on this branch and uploaded there work through

git branch --set-upstream origin BitPankaj

Now in my repository I can see that those branch appears and their commits are also visible:

commits section

and Branches:

branches section

Now each one of them where trying to create a pull request to review there code and merge to master branch. so they tried doing something like this:

But it throws an error of unrelated branches:

The following error(s) occurred saving this pull request: Unrelated branches

pull request error

We people are learning to work on git, help me out with this.

like image 883
Nitish Kumar Avatar asked Sep 11 '17 14:09

Nitish Kumar


Video Answer


2 Answers

This is because the BitPankaj branch is an orphan branch.

That means the branch BitPankaj didn’t created from master branch (as you expected), but may use git checkout --orphan BitPankaj instead. The branch structure looks like:

A---B---…---C   master
D---…---E    BitPankaj

You can double check in your local repo. Update/pull the branches and show logs as graph:

git checkout master
git pull origin master
git checkout BitPankaj
git pull BitPankaj
git log --oneline --decorate --graph --all 
#Or you can use gitk --all if you installed git bash

In order to create pull request between BitPankaj and master, you should change BitPankaj branch based from master branch.

  • To change BitPankaj based from the latest version (commit C) of master branch:

    git rebase master BitPankaj
    git push -f origin BitPankaj
    

    Then the commit history will like:

    A---B---…---C   master
                 \
                  D---…---E   BitPankaj
    
  • To changes BitPankaj branch based from an old version (such as commit B) of master branch:

    git rebase <commit id for B> BitPankaj
    git push -f origin BitPankaj
    

    Then the commit history will like:

    A---B---…---C   master
         \
          D---…---E   BitPankaj
    

Then you can create pull request to merge BitPankaj into master branch on bitbucket successfully.

like image 124
Marina Liu Avatar answered Oct 16 '22 05:10

Marina Liu


You should be able to use --allow-unrelated-histories to force the merge to happen.

You should be able to use it like:-

git pull origin branchname --allow-unrelated-histories

for extra info have a look at:-

Git Merge Documentation

like image 23
Tony Hensler Avatar answered Oct 16 '22 06:10

Tony Hensler