简体   繁体   English

Git 从另一个存储库中拉出一个分支?

[英]Git pulling a branch from another repository?

I have a local git repository which is a clone of a repository on github. Someone has forked the repository and made changes in a new branch on a new repository.我有一个本地 git 存储库,它是 github 上存储库的克隆。有人分叉了存储库并在新存储库的新分支中进行了更改。 I want to move this new branch into my repository (locally to work on it first before merging with the master).我想将这个新分支移动到我的存储库中(在与主分支合并之前首先在本地进行处理)。

I tried creating a new branch and then pulling from the forked repository but it complains because the new branch is a copy of the master branch as well as the local file changes, so it says我尝试创建一个新分支,然后从分叉的存储库中提取,但它会抱怨,因为新分支是主分支的副本以及本地文件更改,所以它说

error: Your local changes to the following files would be overwritten by merge . error: Your local changes to the following files would be overwritten by merge

So how can I pull the branch in the other repository into a new branch on my local repository?那么如何将其他存储库中的分支拉入本地存储库中的新分支呢?

I hope that makes sense.我希望这是有道理的。 If not, this is my repository: https://github.com/MatthewLM/cbitcoin如果没有,这是我的存储库: https://github.com/MatthewLM/cbitcoin

As you can see, someone has created a new repository with the branch "linuxBuild": https://github.com/austonst/cbitcoin/tree/linuxBuild如您所见,有人创建了一个新的存储库,分支为“linuxBuild”: https://github.com/austonst/cbitcoin/tree/linuxBuild

I want that branch on my local repository for MatthewLM/cbitcoin.我想在我的 MatthewLM/cbitcoin 本地存储库中创建该分支。

How can I do this?我怎样才能做到这一点?

You need to make sure that git status shows that no unstaged changes exist in your local repository. 您需要确保git status显示本地存储库中不存在未分级的更改。
You can do this by first stashing your local changes and than pulling that branch. 您可以通过首先存储本地更改而不是拉动该分支来完成此操作。 Afterward you can apply your stash. 之后你可以申请你的藏匿处。


If you want to re-create the branch structure of the fork in your local repository, you can do the following: 如果要在本地存储库中重新创建fork的分支结构,可以执行以下操作:

git remote add fork <url of fork>
git fetch fork
git checkout -b fork_branch fork/<branch>

This will create the local branch fork_branch with the same history like <branch> in the fork, ie fork_branch will branch off of your master where <branch> does in the fork. 这将创建本地分支fork_branch具有与fork中的<branch>相同的历史记录,即fork_branch将从master中分支出来,其中<branch>在fork中执行。 Additionally, your local branch will now track that branch in the fork, so you can easily pull in new changes committed in the fork. 此外,您的本地分支现在将在fork中跟踪该分支,因此您可以轻松地引入fork中提交的新更改。

I think you still need to make sure beforehand that your working copy doesn't contain any changes. 我认为您仍需要事先确定您的工作副本不包含任何更改。

Method without adding remote. 方法无需添加远程。

git checkout --orphan fork_branch
git reset --hard
git pull <url of fork> <branch>

I was able to do something similar using...我能够使用...做类似的事情

git switch -c new_branch
git pull https://github.com/<user>/<forked-repo>.git <branch>

If you want to fetch without tracking from (repository1 > branch1) to (repository2 > branch2),如果你想获取而不跟踪从 (repository1 > branch1) 到 (repository2 > branch2),

  • cd repository2光盘库 2
  • git fetch repository1 branch1:branch2 git 获取 repository1 branch1:branch2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM