views:

1200

answers:

4

I have a project with multiple branches. I've been pushing them to github, and now that someone else is working on them i need to do a pull from github. It works fine in master. But say I have 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: http://stackoverflow.com/questions/1072261/push-and-pull-branches-in-git

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

Any suggestions?

A: 

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

git pull origin BRANCH

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

Alex N.
+1  A: 

you could pull a branch to a branch with the following command

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

git pull origin xyz:xyz

When you are on the master branch you also could first checkout a branch like:

git checkout -b xyz this creates a new branch "xyz" from the master and directly checks it out. than you do: git pull origin xyz this pulls the new branch to your local xyz branch

Robert Cabri
+1  A: 

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
innaM
This is the answer. Thanks.
gmoore
+3  A: 

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!
mipadi
No, the problem is with fetching, not with merge step.
Jakub Narębski
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?
mipadi