Pull a certain branch from the remote server

Viewed 1938863

Say that someone created a branch xyz. How do I pull the branch xyz from the remote server (e.g. GitHub) and merge it into an existing branch xyz in my local repo?

The answer to Push branches to Git gives me the error "! [rejected]" and mentions "non fast forward".

15 Answers

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

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.

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.

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"

The Local User Needs to Update Both the Master Branch and the XYZ Branches Separately

None of these posts answers the original question!

How do I pull the branch xyz from the remote server (e.g. GitHub) and merge it into an existing branch xyz in my local repo? The answer to Push branches to Git gives me the error "! [rejected]" and mentions "non fast forward".

If you want to merge a remote xyz branch into a local xyz branch, where both branches exist off a master branch in a remote and local repository, just select or "checkout" the local xyz branch first, then do a "pull" on the same remote branch. Simple!

git checkout xyz
git pull origin xyz

But, the user got an error! That update did not work. Why?

If this is failing, it has nothing to do with the two xyz branches on remote and local repositories not being able to merge. It sounds like the master branch on the remote repository has been changed with new commits the local repository does not have. The fast-forward message likely means that the xyz branch on the remote repo cannot update the local repo's xyz branch until the local repo gets all the changes added to its master branch from the remote repo first.

These could be new commits or changes the other developer added to the remote master branch, who then ran a rebase on their xyz branch to move it to the end of those new commit HEADS in the remote master. The local repo user has no way to merge that change as its master branch is missing those new added commits at the end, so cannot update the xyz rebase change until its local master branch on its repo is updated with a merge via a pull, first.

So go ahead and update the local master with the remote master's updates first using a pull, then try your xyz branch pull again...

git checkout master
git pull origin

git checkout xyz
git pull origin xyz

Remember, a pull is just a fetch then merge from a remote repo to your local repo on whatever branch you are currently focused on.

These work for me.

  1. To pull a specific remote branch to the current local branch you are in. (Where <remote_repo> is the remote repository and <remote_branch> is the specific remote branch you want to pull)
git pull <remote_repo> <remote_branch>

e.g.

git pull origin remote_master
  1. To pull a specific remote branch to a specific local branch. (Where <local_branch> is the specific local branch you want to pull into)
git pull <remote_repo> <remote_branch>:<local_branch>

e.g.

git pull origin remote_master:local_master
Related