从远程服务器拉取某个分支

发布于 2024-08-10 11:50:03 字数 319 浏览 4 评论 0原文

假设有人创建了一个分支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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

尛丟丟 2024-08-17 11:50:03

但是我收到错误“![拒绝]”以及有关“非快进”的信息

这是因为 Git 无法将分支中的更改合并到当前的主控中。假设您已签出分支 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 将导致快进合并:

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!

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 pull origin other-branch

Git is basically doing this:

$ 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 only merge other-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 merging other-branch would result in a fast-forward merge:

O-O-O-O-O-O
^         ^
master    other-branch

However, this would not be a fast-forward merge:

    v master
O-O-O
\
 \-O-O-O-O
         ^ other-branch

To solve your problem, first fetch the 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:

$ git merge origin/other-branch
# Fix merge conflicts, if they occur
# Add merge conflict fixes
$ git commit    # And commit the merge!
独享拥抱 2024-08-17 11:50:03

只需显式跟踪您的远程分支,一个简单的 git pull 即可完成您想要的操作:

git branch -f remote_branch_name origin/remote_branch_name
git checkout remote_branch_name

后者是本地操作。

或者更适合 有关分叉的 GitHub 文档

git branch -f new_local_branch_name upstream/remote_branch_name

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
千里故人稀 2024-08-17 11:50:03

一种安全的方法是首先创建本地分支(即 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

A safe approach is to create a local branch (i.e. xyz) first and then pull the remote branch into your locals.

# 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

Here is the syntax that could pull a remote branch to a local branch.

git pull {repo} {remotebranchname}:{localbranchname}

git pull origin xyz:xyz
树深时见影 2024-08-17 11:50:03

最好的办法是:

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

The best way is:

git checkout -b <new_branch> <remote repo name>/<new_branch>
没有心的人 2024-08-17 11:50:03

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

忆沫 2024-08-17 11:50:03

我不确定我完全理解这个问题,但是拉动现有分支是这样完成的(至少它对我有用:)

git pull origin BRANCH

这是假设您的本地分支是从 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 :)

git pull origin BRANCH

This is assuming that your local branch is created off of the origin/BRANCH.

诗化ㄋ丶相逢 2024-08-17 11:50:03

简而言之,如果您想从 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

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
丑疤怪 2024-08-17 11:50:03

这帮助我在将远程分支合并到其他分支之前获取它:

git fetch repo xyz:xyz
git checkout xyz

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

git fetch repo xyz:xyz
git checkout xyz
半﹌身腐败 2024-08-17 11:50:03

要从 GitHub 拉取分支,您可以使用

git checkout --track origin/the-branch-name

确保分支名称完全相同。

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.

2024-08-17 11:50:03

这些对我有用。

  1. 将特定的远程分支拉取到您所在的当前本地分支。
    (其中 是远程存储库, 是您要拉取的特定远程分支)
git pull <remote_repo> <remote_branch>

例如,

git pull origin remote_master
  1. 将特定远程分支拉取到特定本地分支。
    (其中 是您要拉入的特定本地分支)
git pull <remote_repo> <remote_branch>:<local_branch>

例如

git pull origin remote_master:local_master

These work for me.

  1. To pull a specific remote branch to the current local branch you are in.
    (Where <remote_repo> is the remote repository and <remote_branch> is the specific remote branch you want to pull)
git pull <remote_repo> <remote_branch>

e.g.

git pull origin remote_master
  1. To pull a specific remote branch to a specific local branch.
    (Where <local_branch> is the specific local branch you want to pull into)
git pull <remote_repo> <remote_branch>:<local_branch>

e.g.

git pull origin remote_master:local_master
夏末 2024-08-17 11:50:03

我做了

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。我想它们是等价的。

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.

葬﹪忆之殇 2024-08-17 11:50:03

您也可以

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

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
峩卟喜欢 2024-08-17 11:50:03
git pull <gitreponame> <branchname>

通常,如果您只将存储库分配给您的代码,那么 gitreponame 将是 origin。

如果您正在处理两个存储库,例如一个是本地存储库,另一个是远程存储库,您可以从 git remote -v 检查存储库列表。这显示了有多少存储库分配给您当前的代码。

BranchName 应存在于相应的 gitreponame 中。

您可以使用以下两个命令来添加或删除存储库

git remote add <gitreponame> <repourl>
git remote remove <gitreponame>
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>
猛虎独行 2024-08-17 11:50:03

将新分支从远程获取到本地的简单解决方案

  1. 获取远程分支

    git fetch xyz

  2. 本地切换到该分支

    git 切换 xyz

  3. 最后,通过运行检查 xyz 是否出现在本地分支上

    git 分支

A simple solution to get new branch from remote to local

  1. fetching the remote branch

    git fetch xyz

  2. switch to that branch locally

    git switch xyz

  3. finally, check if xyz appears on your local branches by running

    git branch

故事与诗 2024-08-17 11:50:03

您可以尝试使用

git branch -a

或 git fetch 来获取最新的分支列表。

git checkout theBranch //进入分支

git checkout -b "yourNewBranch" // 在你自己的分支“theBranch”中工作

you can try with

git branch -a

or git fetch to get latest list of branches.

git checkout theBranch //go in to the branch

git checkout -b "yourNewBranch" // to work in your own branch in base "theBranch"

三寸金莲 2024-08-17 11:50:03

尝试这个

  • 第1步:签出到本地分支“xyz”git checkout
  • 第2步:获取更改但不直接更新。所以使用“git fetch” 用于 xyz 更新本地存储库。
  • 第3步:合并并修复冲突(如果有)。 “git 合并”。

Try this

  • 1 step : checkout to local branch "xyz" git checkout
  • 2 step : fetch the changes but not update like directly.So use "git fetch" for xyz to update local repositary.
  • 3 step : merge and fix the conflict if there is any. "git merge".
转角预定愛 2024-08-17 11:50:03

本地用户需要分别更新主分支和 XYZ 分支

这些帖子都没有回答原始问题!

如何从远程服务器(例如 GitHub)拉取分支 xyz 并
将其合并到本地存储库中的现有分支 xyz 中?
Push Branches to Git 的答案给了我错误“![拒绝]”
并提到“非快进”。

如果您想将远程 xyz 分支合并到本地 xyz 分支,其中两个分支都存在于远程和本地存储库中的 master 分支之外,只需选择或“签出”首先本地 xyz 分支,然后在同一个远程分支上执行“拉取”操作。简单的!

git checkout xyz
git pull origin xyz

但是,用户遇到了错误!该更新不起作用。为什么?

如果失败,则与远程和本地存储库上的两个 xyz 分支无法合并无关。听起来远程存储库上的 ma​​ster 分支 已被本地存储库没有的新提交更改。快进消息可能意味着远程存储库上的 xyz 分支无法更新本地存储库的 xyz 分支,直到本地存储库获取添加到其的所有更改。首先来自远程存储库的主分支。

这些可能是其他开发人员添加到远程 master 分支的新提交或更改,然后他们在他们的 xyz 分支上运行变基以将其移动到远程 master 中这些新提交 HEADS 的末尾。本地存储库用户无法合并该更改,因为其主分支在末尾缺少那些新添加的提交,因此无法更新 xyz 变基更改,直到其存储库上的本地主分支更新为首先通过pull进行合并。

因此,请先使用pull使用远程master的更新来更新本地master,然后再次尝试xyz分支pull...

git checkout master
git pull origin

git checkout xyz
git pull origin xyz

记住,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!

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".

If you want to merge a remote xyz branch into a local xyz branch, where both branches exist off a master branch in a remote and local repository, just select or "checkout" the local xyz branch first, then do a "pull" on the same remote branch. Simple!

git checkout xyz
git pull origin xyz

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 the xyz branch on the remote repo cannot update the local repo's xyz 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 the xyz rebase change until its local master branch on its repo is updated with a merge via a pull, first.

So go ahead and update the local master with the remote master's updates first using a pull, then try your xyz branch pull again...

git checkout master
git pull origin

git checkout xyz
git pull origin xyz

Remember, a pull is just a fetch then merge from a remote repo to your local repo on whatever branch you are currently focused on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文