我已通过
git fetch --all
拉取了所有远程分支。我可以通过git branch -a
看到我想合并的分支,它存储在remotes/origin/branchname中。但问题是它无法访问,我无法合并或者检出。git fetch --all
拉取了所有远程分支。我可以通过git branch -a
看到我想合并的分支,它存储在remotes/origin/branchname中。但问题是它无法访问,我无法合并或者检出。你可以使用它们的远程名称引用那些远程跟踪分支(在git branch -r
中列出)。
你需要获取远程分支:
git fetch origin aRemoteBranch
如果你想将其中一个远程分支合并到你的本地分支:git checkout aLocalBranch
git merge origin/aRemoteBranch
注1:对于一个有着悠久历史的大型代码仓库,当你使用git fetch
时,你将需要添加--depth=1
选项。
注2:这些命令也适用于其他远程代码仓库,因此,如果你正在处理一个分叉项目,你可以设置一个origin
和一个upstream
。
注3:user3265569在评论中建议以下别名:
从
aLocalBranch
运行git combine remoteBranch
别名:相反的情况:如果你想将你的一个本地分支合并到一个远程分支(与上面展示的从远程分支合并到本地分支相反),你需要首先在所述的远程分支之上创建一个新的本地分支:
combine = !git fetch origin ${1} && git merge origin/${1}
git checkout -b myBranch origin/aBranch git merge anotherLocalBranch
这里的想法是将“你的本地分支之一”(这里是
anotherLocalBranch
)合并到远程分支(origin/aBranch
)中。
为此,您首先创建“myBranch
”表示该远程分支:即git checkout -b myBranch origin/aBranch
部分。
然后您可以将anotherLocalBranch
合并到它(myBranch
)中。
git checkout branch-i-am-working-in
") 然后执行以下命令:git merge origin/branch-i-want-to-merge-from
git fetch origin/branch-i-want-to-merge-from
,是这样吗? - Hinrichgit fetch origin develop
后,紧接着执行 git merge origin/develop
。 - Oliviergit merge
是不够的。 - Samgit add .
-> git commit -m <message>
-> git push -u origin <branch>
吗? - Cloud Cho首先从远程仓库获取分支。
git fetch origin remote_branch_name
将远程分支合并到本地分支
git merge origin/remote_branch_name
或许你想用本地分支追踪远程分支:
git branch -b new-local-branch
git branch --set-upstream-to=origin/remote-branch new-local-branch
git checkout new-local-branch
git pull
如果你已经拉取了远程分支,并且执行 git branch -a
命令,
你会得到如下结果:
* 8.0
xxx
remotes/origin/xxx
remotes/origin/8.0
remotes/origin/HEAD -> origin/8.0
remotes/rep_mirror/8.0
接着,您可以使用rep_mirror/8.0
来指定远程分支的本地版本。
诀窍在于remotes/rep_mirror/8.0
无法工作,但rep_mirror/8.0
可以。
因此,像git merge -m "my msg" rep_mirror/8.0
这样的命令执行合并操作。
(注:这是对@VonC回答的评论。我将其作为另一个答案,因为代码块不适合评论格式)
aLocalBranch
"合并到"myBranch
",其中"myBranch
"代表一个名为origin/aBranch
的远程分支。 - VonCaLocalBranch
运行git combine remoteBranch
。别名:combine = !git fetch origin ${1} && git merge origin/${1}
。 - user3265569