Git 从 GitHub 拉取某个分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1709177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git pull a certain branch from GitHub
提问by tybro0103
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
?
我有一个有多个分支的项目。我一直在将它们推送到GitHub,现在其他人正在处理该项目,我需要从 GitHub 中提取他们的分支。它在 master 中运行良好。但是说有人创建了一个分支xyz
。如何xyz
从 GitHub 中提取分支并将其合并到xyz
我的分支中localhost
?
I actually have my answer here: Push and pull branches in Git
我实际上在这里有我的答案: 在 Git 中推和拉分支
But I get an error "! [rejected]" and something about "non fast forward".
但是我收到一个错误“![拒绝]”和一些关于“非快进”的信息。
Any suggestions?
有什么建议?
回答by mipadi
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 无法将分支的更改合并到您当前的 master 中。假设您已签出 branch master
,并且想要合并远程分支other-branch
。当你这样做时:
$ git pull origin other-branch
Git is basically doing this:
Git基本上是这样做的:
$ 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 onlymerge other-branch
ifit can perform a fast-forwardmerge. A fast-forwardmerge is a merge in which the head of the branch you are trying to merge into is a direct descendentof 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:
也就是说, apull
只是 afetch
后跟 a merge
。然而,当pull
-ing,Git会只合并other-branch
,如果它可以进行快进合并。一个快进合并是合并在您试图合并到分支的头部是一个直接后裔要合并分支的头。例如,如果您有此历史树,则合并other-branch
将导致快进合并:
O-O-O-O-O-O
^ ^
master other-branch
However, this would notbe a fast-forward merge:
但是,这不会是快进合并:
v master
O-O-O
\
\-O-O-O-O
^ other-branch
To solve your problem, first fetchthe 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:
然后将它合并到您当前的分支(我假设是master
),并修复任何合并冲突:
$ git merge origin/other-branch
# Fix merge conflicts, if they occur
# Add merge conflict fixes
$ git commit # And commit the merge!
回答by innaM
Simply track your remote branches explicitly and a simple git pull
will do just what you want:
只需显式跟踪您的远程分支,一个简单的git pull
将做您想要的:
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:
或者更符合GitHub 上的 fork 文档:
git branch -f new_local_branch_name upstream/remote_branch_name
回答by Robert Cabri
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:
当您在 master 分支上时,您还可以先检出一个分支,例如:
git checkout -b xyz
This creates a new branch, "xyz", from the master and directly checks it out.
这会从 master 中创建一个新分支“xyz”并直接将其检出。
Then you do:
然后你做:
git pull origin xyz
This pulls the new branch to your local xyz
branch.
这会将新分支拉到您的本地xyz
分支。
回答by mohit
The best way is:
最好的办法是:
git checkout -b <new_branch> <remote repo name>/<new_branch>
回答by Bradley Flood
回答by Alex N.
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.
这是假设您的本地分支是从源/分支创建的。
回答by anatoly techtonik
This helped me to get remote branch before merging it into other:
这帮助我在将其合并到其他分支之前获得远程分支:
git fetch repo xyz:xyz
git checkout xyz
回答by Adrien Renaud
Simply put, If you want to pull from GitHub the branch the_branch_I_want
:
简单地说,如果你想从 GitHub 拉取分支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
回答by prathap
git pull <gitreponame> <branchname>
Usually if you have only repo assigned to your code then the gitreponame would be origin.
通常,如果您只将 repo 分配给您的代码,那么 gitreponame 将是原点。
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.
如果您正在处理两个仓库,例如一个是本地的,另一个是远程的,您可以从git remote -v检查仓库的列表。这显示了分配给您当前代码的回购数量。
BranchName should exists into corresponding gitreponame.
BranchName 应该存在于相应的 gitreponame 中。
you can use following two commands to add or remove repo's
您可以使用以下两个命令来添加或删除 repo
git remote add <gitreponame> <repourl>
git remote remove <gitreponame>
回答by PKV
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
-r 用于变基。这将使您从分支结构
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.
这将导致更清晰的历史。注意:如果您已经将您的其他分支推送到原点(或任何其他远程),您可能必须在 rebase 后强制推送您的分支。
git push -f origin other-branch