从远程服务器拉取某个分支
假设有人创建了一个分支xyz
。如何从远程服务器拉取分支xyz
(例如GitHub )并将其合并到本地存储库中的现有分支 xyz
中?
答案是 将分支推送到 Git 给我错误“ ![拒绝]”并提到“非快进”。
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".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
这是因为 Git 无法将分支中的更改合并到当前的主控中。假设您已签出分支
master
,并且想要合并到远程分支other-branch
。当你这样做时:Git 基本上是这样做的:
也就是说,
pull
只是一个fetch
,然后是一个merge
。然而,当pull
-ing时,Git只会合并其他分支
如果它可以执行快进合并。 快进合并是一种合并,其中您尝试合并到的分支的头部是您要合并的分支的头部的直接后代。例如,如果您有此历史树,则合并other-branch
将导致快进合并:但是,这不是是快进合并:
要解决您的问题,首先获取远程分支:
然后将其合并到当前分支(我假设是
master
),并修复任何合并冲突: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 branchother-branch
. When you do this:Git is basically doing this:
That is, a
pull
is just afetch
followed by amerge
. However, whenpull
-ing, Git will only mergeother-branch
if it can perform a fast-forward merge. A fast-forward merge is a merge in which the head of the branch you are trying to merge into is a direct descendent of the head of the branch you want to merge. For example, if you have this history tree, then mergingother-branch
would result in a fast-forward merge:However, this would not be a fast-forward merge:
To solve your problem, first fetch the remote branch:
Then merge it into your current branch (I'll assume that's
master
), and fix any merge conflicts:只需显式跟踪您的远程分支,一个简单的 git pull 即可完成您想要的操作:
后者是本地操作。
或者更适合 有关分叉的 GitHub 文档:
Simply track your remote branches explicitly and a simple
git pull
will do just what you want:The latter is a local operation.
Or even more fitting in with the GitHub documentation on forking:
一种安全的方法是首先创建本地分支(即 xyz),然后将远程分支拉入本地分支。
以下是可以将远程分支拉到本地分支的语法。
A safe approach is to create a local branch (i.e. xyz) first and then pull the remote branch into your locals.
Here is the syntax that could pull a remote branch to a local branch.
最好的办法是:
The best way is:
git fetch
将获取最新的分支列表。现在您可以
git checkout MyNewBranch
完成:)
有关更多信息,请参阅文档:git fetch< /a>
git fetch
will grab the latest list of branches.Now you can
git checkout MyNewBranch
Done :)
For more info see docs: git fetch
我不确定我完全理解这个问题,但是拉动现有分支是这样完成的(至少它对我有用:)
这是假设您的本地分支是从 origin/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 :)
This is assuming that your local branch is created off of the origin/BRANCH.
简而言之,如果您想从 GitHub 中提取分支
the-branch-I-want
:Simply put, If you want to pull from GitHub the branch
the-branch-I-want
:这帮助我在将远程分支合并到其他分支之前获取它:
This helped me to get remote branch before merging it into other:
要从 GitHub 拉取分支,您可以使用
确保分支名称完全相同。
for pulling the branch from GitHub you can use
Make sure that the branch name is exactly the same.
这些对我有用。
(其中
是远程存储库,
是您要拉取的特定远程分支)例如,
(其中
是您要拉入的特定本地分支)例如
These work for me.
(Where
<remote_repo>
is the remote repository and<remote_branch>
is the specific remote branch you want to pull)e.g.
(Where
<local_branch>
is the specific local branch you want to pull into)e.g.
我做了
而不是
按照@innaM 的建议。
当我使用上游版本时,它说“致命:不是有效的对象名称:'upstream/remote_branch_name”。我没有按照评论的建议执行 git fetch origin 操作,而是简单地将
upstream
替换为origin
。我想它们是等价的。I did
Instead of
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 replacedupstream
withorigin
. I guess they are equivalent.您也可以
修复合并冲突
如果有任何-r 用于变基, 。
这将使您的分支结构从
到
这将导致更清晰的历史记录。
注意:如果您已经将其他分支推送到原点(或任何其他远程),则可能必须在变基后强制推送您的分支。
you may also do
fix merge conflicts if any
-r is for rebase.
This will make you branch structure from
to
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.
通常,如果您只将存储库分配给您的代码,那么 gitreponame 将是 origin。
如果您正在处理两个存储库,例如一个是本地存储库,另一个是远程存储库,您可以从 git remote -v 检查存储库列表。这显示了有多少存储库分配给您当前的代码。
BranchName 应存在于相应的 gitreponame 中。
您可以使用以下两个命令来添加或删除存储库
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 fetch xyz
本地切换到该分支
git 切换 xyz
最后,通过运行检查 xyz 是否出现在本地分支上
git 分支
A simple solution to get new branch from remote to local
fetching the remote branch
git fetch xyz
switch to that branch locally
git switch xyz
finally, check if xyz appears on your local branches by running
git branch
您可以尝试使用
或 git fetch 来获取最新的分支列表。
git checkout theBranch
//进入分支git checkout -b "yourNewBranch"
// 在你自己的分支“theBranch”中工作you can try with
or
git fetch
to get latest list of branches.git checkout theBranch
//go in to the branchgit checkout -b "yourNewBranch"
// to work in your own branch in base "theBranch"尝试这个
git checkout
git fetch” 用于 xyz 更新本地存储库。
git 合并
”。Try this
git checkout
git fetch
" for xyz to update local repositary.git merge
".本地用户需要分别更新主分支和 XYZ 分支
这些帖子都没有回答原始问题!
如果您想将远程
xyz
分支合并到本地xyz
分支,其中两个分支都存在于远程和本地存储库中的 master 分支之外,只需选择或“签出”首先本地xyz
分支,然后在同一个远程分支上执行“拉取”操作。简单的!但是,用户遇到了错误!该更新不起作用。为什么?
如果失败,则与远程和本地存储库上的两个
xyz
分支无法合并无关。听起来远程存储库上的 master 分支 已被本地存储库没有的新提交更改。快进消息可能意味着远程存储库上的 xyz 分支无法更新本地存储库的 xyz 分支,直到本地存储库获取添加到其的所有更改。首先来自远程存储库的主分支。这些可能是其他开发人员添加到远程 master 分支的新提交或更改,然后他们在他们的 xyz 分支上运行变基以将其移动到远程 master 中这些新提交 HEADS 的末尾。本地存储库用户无法合并该更改,因为其主分支在末尾缺少那些新添加的提交,因此无法更新
xyz
变基更改,直到其存储库上的本地主分支更新为首先通过pull
进行合并。因此,请先使用xyz分支
pull
使用远程master的更新来更新本地master,然后再次尝试pull
...记住,
pull
只是fetch
,然后从远程存储库合并
到您当前关注的任何分支上的本地存储库。The Local User Needs to Update Both the Master Branch and the XYZ Branches Separately
None of these posts answers the original question!
If you want to merge a remote
xyz
branch into a localxyz
branch, where both branches exist off a master branch in a remote and local repository, just select or "checkout" the localxyz
branch first, then do a "pull" on the same remote branch. Simple!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 thexyz
branch on the remote repo cannot update the local repo'sxyz
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 thexyz
rebase change until its local master branch on its repo is updated with a merge via apull
, first.So go ahead and update the local master with the remote master's updates first using a
pull
, then try yourxyz
branchpull
again...Remember, a
pull
is just afetch
thenmerge
from a remote repo to your local repo on whatever branch you are currently focused on.