Git pull a certain branch from GitHub

2020-01-25 03:08发布

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?

11条回答
成全新的幸福
2楼-- · 2020-01-25 03:17

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.

查看更多
男人必须洒脱
3楼-- · 2020-01-25 03:22

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.

查看更多
啃猪蹄的小仙女
4楼-- · 2020-01-25 03:22

git fetch will grab the latest list of branches.

Now you can git checkout MyNewBranch

Done :)


For more info see docs: git fetch

查看更多
时光不老,我们不散
5楼-- · 2020-01-25 03:23

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.

查看更多
ら.Afraid
6楼-- · 2020-01-25 03:30

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

git fetch repo xyz:xyz
git checkout xyz
查看更多
Bombasti
7楼-- · 2020-01-25 03:36

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
查看更多
登录 后发表回答