Git:在本地合并远程分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21651185/
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: Merge a Remote branch locally
提问by micahblu
I've pulled all remote branches via git fetch --all
. I can see the branch I'd like to merge via git branch -a
as remotes/origin/branchname. Problem is its not accessible. I can't merge or checkout?
我已经通过git fetch --all
. 我可以看到我想合并的分支git branch -a
作为 remotes/origin/branchname。问题是它无法访问。我无法合并或结帐?
回答by VonC
You can reference those remote tracking branches ~(listed with git branch -r
) with the name of their remote.
您可以使用远程跟踪分支git branch -r
的名称来引用这些远程跟踪分支 ~(以 列出)。
You need to fetch the remote branch:
您需要获取远程分支:
git fetch origin aRemoteBranch
If you want to merge one of those remote branches on your local branch:
如果您想在本地分支上合并这些远程分支之一:
git checkout master
git merge origin/aRemoteBranch
Note 1:For a large repo with a long history, you will want to add the --depth=1
option when you use git fetch
.
注意 1:对于历史悠久的大型 repo,您将需要--depth=1
在使用git fetch
.
Note 2:These commands also work with other remote repos so you can setup an origin
and an upstream
if you are working on a fork.
注意 2:这些命令也适用于其他远程存储库,因此您可以设置 anorigin
和 anupstream
如果您正在使用 fork。
Opposite scenario: If you want to merge one of your local branch on a remote branch (as opposed to a remote branch to a local one, as shown above), you need to create a newlocal branch on top of said remote branch first:
相反的场景:如果你想在远程分支上合并一个本地分支(而不是远程分支到本地分支,如上所示),你需要先在所述远程分支之上创建一个新的本地分支:
git checkout -b myBranch origin/aBranch
git merge anotherLocalBranch
The idea here, is to merge "one of your local branch" (here anotherLocalBranch
) to a remote branch (origin/aBranch
).
For that, you create first "myBranch
" as representing that remote branch: that is the git checkout -b myBranch origin/aBranch
part.
And thenyou can merge anotherLocalBranch
to it (to myBranch
).
这里的想法是将“您的本地分支之一”(此处anotherLocalBranch
)合并到远程分支 ( origin/aBranch
)。
为此,您首先创建“ myBranch
”来表示该远程分支:这就是git checkout -b myBranch origin/aBranch
部分。
而且那么您可以合并anotherLocalBranch
到它(到myBranch
)。
回答by Michael Dautermann
Whenever I do a merge, I get into the branch I want to merge into (e.g. "git checkout branch-i-am-working-in
") and then do the following:
每当我进行合并时,我都会进入要合并的分支(例如“ git checkout branch-i-am-working-in
”),然后执行以下操作:
git merge origin/branch-i-want-to-merge-from
git merge origin/branch-i-want-to-merge-from
回答by Lanil Marasinghe
Fetch the remote branch from the origin first.
首先从原点获取远程分支。
git fetch origin remote_branch_name
Merge the remote branch to the local branch
将远程分支合并到本地分支
git merge origin/remote_branch_name
回答by e18r
Maybe you want to trackthe remote branch with a local branch:
也许您想使用本地分支跟踪远程分支:
- Create a new local branch:
git branch new-local-branch
- Set this newly created branch to track the remote branch:
git branch --set-upstream-to=origin/remote-branch new-local-branch
- Enter into this branch:
git checkout new-local-branch
- Pull all the contents of the remote branch into the local branch:
git pull
- 创建一个新的本地分支:
git branch new-local-branch
- 设置这个新创建的分支来跟踪远程分支:
git branch --set-upstream-to=origin/remote-branch new-local-branch
- 进入这个分支:
git checkout new-local-branch
- 将远程分支的所有内容拉入本地分支:
git pull
回答by herve-guerin
If you already fetched your remote branch and do git branch -a
,
you obtain something like :
如果您已经获取了远程分支并执行了操作git branch -a
,
您将获得如下内容:
* 8.0
xxx
remotes/origin/xxx
remotes/origin/8.0
remotes/origin/HEAD -> origin/8.0
remotes/rep_mirror/8.0
After that, you can use rep_mirror/8.0
to designate locally your remote branch.
之后,您可以使用rep_mirror/8.0
在本地指定您的远程分支。
The trick is that remotes/rep_mirror/8.0
doesn't work but rep_mirror/8.0
does.
诀窍是这remotes/rep_mirror/8.0
不起作用,但rep_mirror/8.0
确实如此。
So, a command like git merge -m "my msg" rep_mirror/8.0
do the merge.
所以,像git merge -m "my msg" rep_mirror/8.0
执行合并这样的命令。
(note : this is a comment to @VonC answer. I put it as another answer because code blocks don't fit into the comment format)
(注意:这是对@VonC 答案的评论。我把它作为另一个答案,因为代码块不适合评论格式)