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上的存储库的克隆。有人分叉了存储库并在新存储库的新分支中进行了更改。我想将这个新分支移动到我的存储库中(在与主服务器合并之前,首先在本地处理它)。
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
.
错误:合并将覆盖对以下文件的本地更改。
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?
我怎样才能做到这一点?
2 个解决方案
#1
115
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,其中包含与fork中的
I think you still need to make sure beforehand that your working copy doesn't contain any changes.
我认为您仍需要事先确定您的工作副本不包含任何更改。
#2
9
Method without adding remote.
方法无需添加远程。
git checkout --orphan fork_branch
git reset --hard
git pull <url of fork> <branch>
#1
115
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,其中包含与fork中的
I think you still need to make sure beforehand that your working copy doesn't contain any changes.
我认为您仍需要事先确定您的工作副本不包含任何更改。
#2
9
Method without adding remote.
方法无需添加远程。
git checkout --orphan fork_branch
git reset --hard
git pull <url of fork> <branch>