简体   繁体   English

拉入不同的Git分支

[英]Pulling in different Git branch

Say I create a repo and it has one branch called master. 假设我创建了一个仓库,它有一个分支称为master。 If someone else forks the repo and pushes to a new branch B, what is the best way for me to pull in B locally without overwriting my local master branch? 如果有人分叉仓库并推送到新的分支B,那么对我来说,在不覆盖本地主分支的情况下在本地引入B的最佳方法是什么? Do I switch to branch B locally and then pull in the changes? 我是否在本地切换到分支B,然后提取更改? My guess is if I don't explicitly switch branches locally then if I pull in the changes from the remote branch B then I will overwrite my local master branch with B. What is the best option here? 我的猜测是,如果我不明确在本地切换分支,那么如果我从远程分支B中提取更改,那么我将用B覆盖本地主分支。这里的最佳选择是什么?

You can do a git fetch origin branchname . 您可以执行git fetch origin branchname By doing a git pull origin branchname you can merge the changes of both the branches. 通过执行git pull origin branchname您可以合并两个分支的更改。 install a mergetool like kdiff3. 安装像kdiff3这样的mergetool。 You can integrate the two, follow this link 您可以将两者整合,请点击此链接

git fetch origin
git checkout --track origin/<remote_branch_name>

You can use git fetch origin branchB to get all commits from branchB without modifying your master or your working directory. 您可以使用git fetch origin branchB来从branchB获取所有提交,而无需修改您的master目录或工作目录。

If you want to apply the changes to your master , you can merge them using git merge FETCH_HEAD while you are on your master branch. 如果要将更改应用到master ,则可以在master分支上使用git merge FETCH_HEAD进行git merge FETCH_HEAD This creates a new commit with all the changes from master and from branchB . 这将创建一个新提交,其中包含masterbranchB所有更改。 FETCH_HEAD is the last commit that was fetched. FETCH_HEAD是最后获取的提交。

I suppose that when you write "[someone] pushes to a new branch B", you mean that they push to their fork repo, not to the original repo origin . 我想当您编写“ [某人]推送到新分支B”时,您的意思是他们推送到了自己的分叉存储库,而不是原始存储库origin

Rather than fetching B from origin, you need to fetch from their fork: 与其从原点获取B ,还需要从他们的fork中获取:

git fetch <url/to/their/fork/repo> B

You will get a copy of the commits in their branch B , and you can see the remote branch with git branch -a . 您将在其分支B中获得提交的副本,并且可以看到带有git branch -a的远程分支。 You can checkout the remote branch to make your working files reflect branch B . 您可以checkout远程分支以使您的工作文件反映分支B

If you want, you can create a local branch at the same time you are fetching. 如果需要,可以在提取的同时创建本地分支。 The name of the created local branch comes after a : : 创建的本地分支的名称位于:

git fetch <url/to/their/fork/repo> B:B

Now instead of running git checkout on a rather long branch name, you can just git checkout B . 现在,您无需在相当长的分支名称上运行git checkout ,而只需git checkout B

Remember that pull is just fetch + merge , and that merge happens with the branch that is currently checked out. 请记住, pull只是fetch + merge ,并且merge发生在当前已签出的分支中。 Your guess is kind of correct in that if you run git pull while master is checked out, you will merge the content of the pulled branch into master . 您的猜测是正确的,因为如果在检出master情况下运行git pull ,则会将pull分支的内容合并到master This is not really overwriting though, rather adding a commit to master . 不过,这并不是真正的覆盖,而是向master添加一个提交。


If you are going to use their fork repo more times, you can add it as a remote: 如果您打算多次使用他们的分叉仓库,则可以将其添加为远程仓库:

git remote add their_fork <url/to/their/fork/repo>

After this you do not need to write their whole URL, you can use the remote name instead: 之后,您无需编写其整个URL,可以改用远程名称:

git fetch their_fork B:B

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

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