Git 从 GitHub 拉取某个分支 [英] Git pull a certain branch from GitHub

查看:49
本文介绍了Git 从 GitHub 拉取某个分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个分支的项目.我一直在将它们推送到 GitHub,现在其他人正在开发我需要的项目从 GitHub 拉取他们的分支.它在 master 中运行良好.但是假设有人创建了一个分支xyz.如何从 GitHub 拉取分支 xyz 并将其合并到我的 localhost 上的分支 xyz 中?

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?

我实际上在这里有我的答案:在 Git 中推送和拉取分支

I actually have my answer here: Push and pull branches in Git

但我收到一个错误![拒绝]"和一些关于非快进"的信息.

But I get an error "! [rejected]" and something about "non fast forward".

有什么建议吗?

推荐答案

但我收到一个错误![拒绝]"和一些关于非快进"的信息

But I get an error "! [rejected]" and something about "non fast forward"

那是因为 Git 无法将分支的更改合并到您当前的 master 中.假设您已经签出分支 master,并且您想要合并到远程分支 other-branch.当你这样做时:

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 基本上是这样做的:

Git is basically doing this:

$ git fetch origin other-branch && git merge other-branch

也就是说,pull 只是一个 fetch 后跟一个 merge.然而,当pull-ing时,Git将only合并other-branch 如果它可以执行一个快进合并.快进合并是这样一种合并,其中您尝试合并的分支的头部是要合并的分支头部的直接后代.例如,如果您有这个历史树,那么合并 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

然后将其合并到您当前的分支中(我假设它是 master),并修复所有合并冲突:

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!

这篇关于Git 从 GitHub 拉取某个分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆