如何将本地主分支合并到本地分支

24

我有一个远程的origin/master以及一个叫做remote_branch的远程分支。 我还有一个本地的master和一个叫做local_branch的本地分支。当我尝试使用git pull master local_branch将本地的master合并进local_branch时,会得到以下提示。

fatal: 'master' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

但是,当我执行 git branch 命令时,我看到了这个:

* loca_branch
  master

为什么我不能从本地的master分支拉取代码?


1
你不会将一个分支拉到另一个分支上,而是要将它们合并。 - Frodon
@Frodon 如果我执行 git fetch(为了稍后执行 git merge),它会获取 origin/master。但我想让 git 查找本地的 master... - four-eyes
@Frodon 是正确的,你的问题不清楚,你想要实现什么? - Shmulik Klein
@Stophface,那么您可能想要将origin/master合并到local_branch上:git fetch && git merge origin/master local_branchpull命令是fetchmerge的快捷方式。 - Frodon
3个回答

62

将来自local_branch的更改合并主分支master中

git checkout master
git merge local_branch

将来自主分支的更改合并本地分支

git checkout local_branch
git merge master

拉取(pull)是指在你拥有一个“源”代码库时


3
这是一次合并操作,不是拉取操作。两者有所不同。 - Guilhem Fry

11

git pullgit fetch && git merge 的别名。你无法从本地分支获取(fetch)(只能从远程仓库获取),实际上如果你的目的是将 master 分支合并到 local_branch 分支中,那么当你在 local_branch 分支上时,只需使用 git merge master 命令即可,不需要使用 fetch。


1
谢谢。我不知道pushpull只用于远程操作。 - four-eyes
我会接受下面的答案,因为他/她先写了并且回答了我的问题。抱歉 :-/ 为此,我需要再一分钟。 - four-eyes

0

正如错误消息所说的那样,master不是它知道的一个存储库。这是因为使用 git pull master local_branch 命令时,您要说“从远程存储库 master 获取local_branch分支并将其合并到我的当前签出分支中”。

但那不是你想要的。你想说“将我的本地分支 master 合并到我的本地分支 local_branch 中,在检查它是否已经签出”,这将是 git checkout local_branch && git merge master


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接