Git pulling a branch from another repository?

旧巷老猫 提交于 2019-12-03 00:14:17

问题


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).

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

As you can see, someone has created a new repository with the branch "linuxBuild": https://github.com/austonst/cbitcoin/tree/linuxBuild

I want that branch on my local repository for MatthewLM/cbitcoin.

How can I do this?


回答1:


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.


If you want to re-create the branch structure of the fork in your local repository, you can do the following:

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.

I think you still need to make sure beforehand that your working copy doesn't contain any changes.




回答2:


Method without adding remote.

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


来源:https://stackoverflow.com/questions/14383212/git-pulling-a-branch-from-another-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!