Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git pull a certain branch from GitHub

I have a project with multiple branches. I've been pushing them to GitHub, and now that someone else is working on the project I need to pull their branches from GitHub. It works fine in master. But say that someone created a branch xyz. How can I pull branch xyz from GitHub and merge it into branch xyz on my localhost?

I actually have my answer here: Push and pull branches in Git

But I get an error "! [rejected]" and something about "non fast forward".

Any suggestions?

like image 555
tybro0103 Avatar asked Nov 10 '09 16:11

tybro0103


People also ask

Can you pull a specific branch from GitHub?

You need to checkout the branch. git pull origin todo-mvvm-databinding will fetch and merge this branch into your local one.

How do I pull from a feature branch?

First, you need to make sure your local main is synchronized with the upstream main . Then, you merge the feature branch into main and push the updated main back to the central repository. Pull requests can be facilitated by product repository management solutions like Bitbucket Cloud or Bitbucket Server.


1 Answers

But I get an error "! [rejected]" and something about "non fast forward"

That's because Git can't merge the changes from the branches into your current master. Let's say you've checked out branch master, and you want to merge in the remote branch other-branch. When you do this:

$ git pull origin other-branch 

Git is basically doing this:

$ git fetch origin other-branch && git merge other-branch 

That is, a pull is just a fetch followed by a merge. However, when pull-ing, Git will only merge other-branch if it can perform a fast-forward merge. A fast-forward merge is a merge in which the head of the branch you are trying to merge into is a direct descendent of the head of the branch you want to merge. For example, if you have this history tree, then merging other-branch would result in a fast-forward merge:

O-O-O-O-O-O ^         ^ master    other-branch 

However, this would not be a fast-forward merge:

    v master O-O-O \  \-O-O-O-O          ^ other-branch 

To solve your problem, first fetch the remote branch:

$ git fetch origin other-branch 

Then merge it into your current branch (I'll assume that's master), and fix any merge conflicts:

$ git merge origin/other-branch # Fix merge conflicts, if they occur # Add merge conflict fixes $ git commit    # And commit the merge! 
like image 79
mipadi Avatar answered Oct 06 '22 13:10

mipadi