我已经通过
git fetch --all
拉取了所有远程分支。我可以通过 git branch -a
看到我想要合并的分支作为remotes/origin/branchname。问题是它无法访问。我无法合并或结帐。
您可以引用那些远程跟踪分支〜(用
git branch -r
列出)及其远程名称。
您需要获取远程分支:
git fetch origin aRemoteBranch
如果您想将这些远程分支之一合并到本地分支上:
git checkout aLocalBranch
git merge origin/aRemoteBranch
注 1: 对于历史悠久的大型仓库,您需要在使用
--depth=1
时添加 git fetch
选项。
注 2: 这些命令还可以与其他远程存储库配合使用,因此如果您正在处理 fork,则可以设置
origin
和 upstream
。
注3:user3265569建议使用以下别名在评论中:
从
出发,运行aLocalBranch
git combine remoteBranch
别名:combine = !git fetch origin ${1} && git merge origin/${1}
相反的场景:如果您想将本地分支之一合并到远程分支(而不是远程分支到本地分支,如上所示),您需要在所述远程分支之上创建一个 new 本地分支首先分支:
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 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答案的评论。我把它作为另一个答案,因为代码块不适合评论格式)