Git pull a certain branch from GitHub

GitMergeBranchGithubPull

Git Problem Overview


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: https://stackoverflow.com/questions/1072261/push-and-pull-branches-in-git

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

Any suggestions?

Git Solutions


Solution 1 - Git

> 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!

Solution 2 - Git

Simply track your remote branches explicitly and a simple git pull will do just what you want:

git branch -f remote_branch_name origin/remote_branch_name
git checkout remote_branch_name

The latter is a local operation.

Or even more fitting in with the GitHub documentation on forking:

git branch -f new_local_branch_name upstream/remote_branch_name

Solution 3 - Git

A safe approach is to create a local branch (i.e. xyz) first and then pull the remote branch into your locals.

# create a local branch
git checkout -b xyz

# make sure you are on the newly created branch
git branch

# finally pull the remote branch to your local branch
git pull origin xyz

Here is the syntax that could pull a remote branch to a local branch.

git pull {repo} {remotebranchname}:{localbranchname}

git pull origin xyz:xyz

Solution 4 - Git

The best way is:

git checkout -b <new_branch> <remote repo name>/<new_branch>

Solution 5 - Git

git fetch will grab the latest list of branches.

Now you can git checkout MyNewBranch

Done :)


For more info see docs: git fetch

Solution 6 - Git

I am not sure I fully understand the problem, but pulling an existing branch is done like this (at least it works for me :)

git pull origin BRANCH

This is assuming that your local branch is created off of the origin/BRANCH.

Solution 7 - Git

Simply put, If you want to pull from GitHub the branch the_branch_I_want:

git fetch origin
git branch -f the_branch_I_want origin/the_branch_I_want
git checkout the_branch_I_want

Solution 8 - Git

This helped me to get remote branch before merging it into other:

git fetch repo xyz:xyz
git checkout xyz

Solution 9 - Git

for pulling the branch from GitHub you can use

git checkout --track origin/the-branch-name

Make sure that the branch name is exactly the same.

Solution 10 - Git

I did

git branch -f new_local_branch_name origin/remote_branch_name

Instead of

git branch -f new_local_branch_name upstream/remote_branch_name

As suggested by @innaM. When I used the upstream version, it said 'fatal: Not a valid object name: 'upstream/remote_branch_name''. I did not do git fetch origin as a comment suggested, but instead simply replaced upstream with origin. I guess they are equivalent.

Solution 11 - Git

you may also do

git pull -r origin master

fix merge conflicts if any

git rebase --continue

-r is for rebase. This will make you branch structure from

        v  master       
o-o-o-o-o
     \o-o-o
          ^ other branch

to

        v  master       
o-o-o-o-o-o-o-o
              ^ other branch
          

This will lead to a cleaner history. Note: In case you have already pushed your other-branch to origin( or any other remote), you may have to force push your branch after rebase.

git push -f origin other-branch

Solution 12 - Git

git pull <gitreponame> <branchname>

Usually if you have only repo assigned to your code then the gitreponame would be origin.

If you are working on two repo's like one is local and another one for remote like you can check repo's list from git remote -v. this shows how many repo's are assigned to your current code.

BranchName should exists into corresponding gitreponame.

you can use following two commands to add or remove repo's

git remote add <gitreponame> <repourl>
git remote remove <gitreponame>

Solution 13 - Git

you can try with

git branch -a

or git fetch to get latest list of branches.

git checkout theBranch //go in to the branch

git checkout -b "yourNewBranch" // to work in your own branch in base "theBranch"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontybro0103View Question on Stackoverflow
Solution 1 - GitmipadiView Answer on Stackoverflow
Solution 2 - GitinnaMView Answer on Stackoverflow
Solution 3 - GitRobert CabriView Answer on Stackoverflow
Solution 4 - GitmohitView Answer on Stackoverflow
Solution 5 - GitBradley FloodView Answer on Stackoverflow
Solution 6 - GitAlex N.View Answer on Stackoverflow
Solution 7 - GitAdrien RenaudView Answer on Stackoverflow
Solution 8 - Gitanatoly techtonikView Answer on Stackoverflow
Solution 9 - GitFrádely DilonéView Answer on Stackoverflow
Solution 10 - GitVickyView Answer on Stackoverflow
Solution 11 - GitPKVView Answer on Stackoverflow
Solution 12 - GitprathapView Answer on Stackoverflow
Solution 13 - GittitleLoginView Answer on Stackoverflow