Git pull a certain branch from GitHub

1,739,653

Solution 1

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

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

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

The best way is:

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

Solution 5

git fetch will grab the latest list of branches.

Now you can git checkout MyNewBranch

Done :)


For more info see docs: git fetch

Share:
1,739,653
tybro0103
Author by

tybro0103

Coffee junkie, computer nerd, fitness monster, and father of two cool dudes.

Updated on July 08, 2022

Comments

  • tybro0103
    tybro0103 almost 2 years

    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?

  • Anonigan
    Anonigan over 14 years
    No, the problem is with fetching, not with merge step.
  • mipadi
    mipadi over 14 years
    Normally, remotes are set up such that fetches are forced, even if they don't result in a fast-forward commit, so it shouldn't occur on fetch unless the OP changed something with the usual configuration. The fast-forward issue can occur during fetch or merge. What makes you say that the problem is definitely in fetching, and not in merging?
  • Jean Jordaan
    Jean Jordaan over 12 years
    I follow these steps (fetch, merge). Git tells me there's nothing to do. When I try to commit, it falls over moaning about fast-forwards.
  • cregox
    cregox about 12 years
    @mipadi I had same issue as Jean and, while I can't say the remote is setup in the non default way you've mentioned I can say using git fetch -f have fixed my issue! Thanks!
  • Martin Konicek
    Martin Konicek almost 12 years
    If you get 'Not a valid object name: 'origin/remote_branch_name', do 'git fetch origin' first.
  • Costa Michailidis
    Costa Michailidis over 10 years
    Perfect! I just didn't know that syntax: git pull {repo} {remotebranchname}:{localbranchname}. Question, if that pull doesn't work (maybe someone's updated the branch and there would be merge conflicts) what are my options?
  • Phrogz
    Phrogz over 8 years
    I'm downvoting this because it attempts to merge the remote branch into your current branch (e.g. master). This is not what most people want to do, and it's not what the OP asked for. The answer by @mohit is the correct choice.
  • Pawan
    Pawan over 7 years
    Phrogz - looks like this behavior changed in recent versions of Git. I used this before and it worked perfectly well.
  • user5359531
    user5359531 about 7 years
    This merges remote branch xzy into local branch master, which is not what was implied by the original question; "How can I pull branch xyz from GitHub and merge it into branch xyz on my localhost?"
  • TomEberhard
    TomEberhard over 5 years
    After creating a new "dev" branch on github, and trying the above, I got the following error message: "fatal: origin/dev is not a commit and a branch 'dev' cannot be created from it" Solution was to "git fetch" per Bradley Flood's solution below, and then re-running mohit's answer.
  • nishantbhardwaj2002
    nishantbhardwaj2002 over 4 years
    just FYI and my future reference as well, "git pull --rebase origin other-remote-branch" also works if you want to rebase.
  • Brendan Metcalfe
    Brendan Metcalfe over 3 years
    What is you don't want to merge?
  • buraky
    buraky over 3 years
    The answer is seriously misleading!!! I applied the answer without noticing @Phrogz 's comment and now suffering from the problem s/he mentioned. Either which branch it tries to merge should be expressed explicitly or the answer should be deleted !!!!
  • Robert Cabri
    Robert Cabri over 3 years
    @Phrogz,@Letitbe which command did you try? I can change the answer accordingly. Never had the issue of pulling it into master.
  • Admin
    Admin almost 3 years
    it worked fine git fetch, then git checkout remoteBranch
  • Satyam Gondhale
    Satyam Gondhale almost 3 years
    Worked for me. Thanks
  • Jonas
    Jonas over 2 years
    This will pull the branch into your current local branch, so when you are on dev and to git pull origin feature it will pull the feature branch into dev. The person who asked the question wants to make a new branch feature and pull into this branch
  • Wolfgang Blessen
    Wolfgang Blessen over 2 years
    fatal: Cannot force update the current branch.
  • Philip Oakley
    Philip Oakley over 2 years
    The {repo} can also be a URL, so for quick looks at some other repo it can be nicer than having to create a remote first (stick the fetch/pull command in the new branch description;-)
  • milos
    milos about 2 years
    but first you need to do git checkout -b xyz then you do git pull origin xyz