git Git从另一个存储库中拉出一个分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14383212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git pulling a branch from another repository?
提问by Matthew Mitchell
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. I want to move this new branch into my repository (locally to work on it first before merging with the master).
我有一个本地 git 存储库,它是 github 上存储库的克隆。有人分叉了存储库并在新存储库的新分支中进行了更改。我想将这个新分支移动到我的存储库中(在与 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?
我怎样才能做到这一点?
回答by Daniel Hilgarth
You need to make sure that git status
shows that no unstaged changes exist in your local repository.
You can do this by first stashing your local changes and than pulling that branch. Afterward you can apply your stash.
您需要确保git status
本地存储库中不存在未暂存的更改。
您可以通过首先隐藏您的本地更改而不是拉那个分支来做到这一点。之后,您可以应用您的藏匿处。
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, i.e. fork_branch
will branch off of your master
where <branch>
does in the 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_branch
具有与<branch>
fork 中相同历史记录的本地分支,fork_branch
即将从您master
在<branch>
fork中的where分支分支。此外,您的本地分支现在将在 fork 中跟踪该分支,因此您可以轻松地提取在 fork 中提交的新更改。
I think you still need to make sure beforehand that your working copy doesn't contain any changes.
我认为您仍然需要事先确保您的工作副本不包含任何更改。
回答by Alex78191
Method without adding remote.
不加遥控器的方法。
git checkout --orphan fork_branch
git reset --hard
git pull <url of fork> <branch>