Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

git - Git从GitHub拉出一个分支(Git pull a certain branch from GitHub)

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. (我一直把它们推给GitHub ,现在有人正在研究这个项目,我需要从GitHub中取出它们的分支。) It works fine in master. (它在master中工作正常。) But say that someone created a branch xyz . (但是说有人创建了一个分支xyz 。) How can I pull branch xyz from GitHub and merge it into branch xyz on my localhost ? (如何从GitHub中提取分支xyz并将其合并到我的localhost上的分支xyz ?)

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? (有什么建议?)

  ask by tybro0103 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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. (那是因为Git无法将分支的更改合并到当前的主服务器中。) Let's say you've checked out branch master , and you want to merge in the remote branch other-branch . (假设您已经检出了分支master ,并且想要在远程分支other-branch合并。) When you do this: (当你这样做:)

$ 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 . (也就是说, pull只是一个fetch然后是一个merge 。) However, when pull -ing, Git will only merge other-branch if it can perform a fast-forward merge. (但是,当pull ,Git 只会合并other-branch 如果它可以执行快进合并。) 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: (例如,如果您有此历史记录树,那么合并other-branch将导致快进合并:)

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: (然后将它合并到您当前的分支(我将假设它是master ),并修复任何合并冲突:)

$ git merge origin/other-branch
# Fix merge conflicts, if they occur
# Add merge conflict fixes
$ git commit    # And commit the merge!

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...