我有一个有多个分支的项目。我一直将他们推到 GitHub,现在其他人正在从事该项目,我需要从 GitHub 中提取他们的分支。它在master中运行良好。但是假设有人创建了一个分支 xyz
。如何从 GitHub 提取分支 xyz
并将其合并到我的 localhost
上的分支 xyz
?
我实际上在这里有我的答案:Push and pull branches in Git
但我得到一个错误“![拒绝]”和一些关于“非快进”的东西。
有什么建议么?
git fetch origin --force
”,但请在使用之前阅读文档。
但我得到一个错误“![拒绝]”和一些关于“非快进”的东西
那是因为 Git 无法将分支中的更改合并到您当前的 master 中。假设您已签出分支 master
,并且想要合并到远程分支 other-branch
。当你这样做时:
$ git pull origin other-branch
Git基本上是这样做的:
$ git fetch origin other-branch && git merge other-branch
也就是说,pull
只是一个 fetch
后跟一个 merge
。但是,当 pull
-ing 时,Git 将 仅 合并 other-branch
如果它可以执行 快进 合并。 快进合并是一种合并,其中您尝试合并的分支的头部是您要合并的分支头部的直接后代。例如,如果您有这个历史树,那么合并 other-branch
将导致快进合并:
O-O-O-O-O-O
^ ^
master other-branch
但是,这不会是快进合并:
v master
O-O-O
\
\-O-O-O-O
^ other-branch
要解决您的问题,请先获取远程分支:
$ git fetch origin other-branch
然后将其合并到您当前的分支(我假设是 master
),并修复任何合并冲突:
$ git merge origin/other-branch
# Fix merge conflicts, if they occur
# Add merge conflict fixes
$ git commit # And commit the merge!
只需明确地跟踪您的远程分支,一个简单的 git pull
就可以满足您的需求:
git branch -f remote_branch_name origin/remote_branch_name
git checkout remote_branch_name
后者是本地操作。
甚至更适合 GitHub documentation on forking:
git branch -f new_local_branch_name upstream/remote_branch_name
一种安全的方法是先创建一个本地分支(即 xyz),然后将远程分支拉入您的本地。
# create a local branch
git checkout -b xyz
# make sure you are on the newly created branch
git branch
# finally pull the remote branch to your local branch
git pull origin xyz
这是可以将远程分支拉到本地分支的语法。
git pull {repo} {remotebranchname}:{localbranchname}
git pull origin xyz:xyz
最好的方法是:
git checkout -b <new_branch> <remote repo name>/<new_branch>
git fetch
将获取最新的分支列表。
现在您可以git checkout MyNewBranch
完毕 :)
有关详细信息,请参阅文档:git fetch
我不确定我是否完全理解这个问题,但是拉一个现有的分支是这样完成的(至少它对我有用:)
git pull origin BRANCH
这是假设您的本地分支是在 origin/BRANCH 之外创建的。
dev
和 git pull origin feature
时,它会将功能分支拉入 dev
。问这个问题的人想创建一个新分支 feature
并拉入这个分支
git checkout -b xyz
然后你做 git pull origin xyz
简单地说,如果您想从 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
这帮助我在将其合并到其他分支之前获得远程分支:
git fetch repo xyz:xyz
git checkout xyz
要从 GitHub 中提取分支,您可以使用
git checkout --track origin/the-branch-name
确保分支名称完全相同。
我做了
git branch -f new_local_branch_name origin/remote_branch_name
代替
git branch -f new_local_branch_name upstream/remote_branch_name
正如@innaM 所建议的那样。当我使用上游版本时,它说'致命:不是有效的对象名称:'upstream/remote_branch_name''。我没有按照评论建议执行 git fetch origin
,而是简单地将 upstream
替换为 origin
。我猜他们是等价的。
git pull <gitreponame> <branchname>
通常,如果您仅将 repo 分配给您的代码,那么 gitreponame 将是 origin。
如果你正在处理两个 repo,一个是本地的,另一个是远程的,你可以从 git remote -v 检查 repo 的列表。这显示了为您当前的代码分配了多少回购。
BranchName 应该存在于相应的 gitreponame 中。
您可以使用以下两个命令来添加或删除 repo
git remote add <gitreponame> <repourl>
git remote remove <gitreponame>
你也可以
git pull -r origin master
修复合并冲突(如果有)
git rebase --continue
-r 用于变基。这将使您的分支结构
v master
o-o-o-o-o
\o-o-o
^ other branch
至
v master
o-o-o-o-o-o-o-o
^ other branch
这将导致更清洁的历史。注意:如果您已经将其他分支推送到原点(或任何其他远程),您可能必须在变基后强制推送您的分支。
git push -f origin other-branch
你可以试试
git branch -a
或 git fetch
获取最新的分支列表。
git checkout theBranch
//进入分支
git checkout -b "yourNewBranch"
// 在你自己的分支“theBranch”中工作
不定期副业成功案例分享
git fetch -f
解决了我的问题!谢谢!xzy
合并到本地分支master
,这不是原始问题所暗示的; “如何从 GitHub 中提取分支 xyz 并将其合并到本地主机上的分支 xyz 中?”