[git] 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?

This question is related to git merge branch github pull

The answer is


git fetch will grab the latest list of branches.

Now you can git checkout MyNewBranch

Done :)


For more info see docs: git fetch


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

git fetch repo xyz:xyz
git checkout xyz

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.


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.


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

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

You could pull a branch to a branch with the following commands.

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.

Then you do:

git pull origin xyz

This pulls the new branch to your local xyz branch.


The best way is:

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

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.


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

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>

Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to merge

Pandas Merging 101 Python: pandas merge multiple dataframes Git merge with force overwrite Merge two dataframes by index Visual Studio Code how to resolve merge conflicts with git? merge one local branch into another local branch Merging dataframes on index with pandas Git merge is not possible because I have unmerged files Git merge develop into feature branch outputs "Already up-to-date" while it's not How merge two objects array in angularjs?

Examples related to branch

Get git branch name in Jenkins Pipeline/Jenkinsfile Why do I have to "git push --set-upstream origin <branch>"? Your configuration specifies to merge with the <branch name> from the remote, but no such ref was fetched.? When does Git refresh the list of remote branches? Fix GitLab error: "you are not allowed to push code to protected branches on this project"? Git push: "fatal 'origin' does not appear to be a git repository - fatal Could not read from remote repository." Git: Merge a Remote branch locally git pull from master into the development branch Depend on a branch or tag using a git URL in a package.json? How can I copy the content of a branch to a new local branch?

Examples related to github

Does the target directory for a git clone have to match the repo name? Issue in installing php7.2-mcrypt How can I switch to another branch in git? How to draw checkbox or tick mark in GitHub Markdown table? How to add a new project to Github using VS Code git clone error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054 How to add empty spaces into MD markdown readme on GitHub? key_load_public: invalid format git - remote add origin vs remote set-url origin Cloning specific branch

Examples related to pull

Your configuration specifies to merge with the <branch name> from the remote, but no such ref was fetched.? How do I force Kubernetes to re-pull an image? Error: Cannot pull with rebase: You have unstaged changes git pull from master into the development branch Trying to git pull with error: cannot open .git/FETCH_HEAD: Permission denied git pull error :error: remote ref is at but expected How to merge remote master to local branch How to undo a git pull? Discard all and get clean copy of latest revision? What is the difference between pull and clone in git?