简体   繁体   English

为什么我需要git merge origin / master中的“master”?

[英]Why do I need the “master” in git merge origin/master?

The convention when using git and you want to get changes from the server is: 使用git并希望从服务器获取更改时的约定是:

git fetch

git merge origin/master

I know there is also git pull , but my specific question is about the syntax origin/master . 我知道还有git pull ,但我的具体问题是关于语法origin/master What does the master part do? master部分做什么? If I just do git merge origin (without the master ) it seems to work. 如果我只是做git merge origin (没有master )它似乎工作。 I know master is a branch, but if I'm tracking more than one branch of a remote, would the normal use case be to merge all of them? 我知道master是一个分支,但是如果我跟踪一个以上的远程分支,那么正常的用例是否会合并所有这些分支?

When a branch is not specified, git merge origin will merge in whatever branch is set as default from the origin remote (generally master ). 当未指定分支时, git merge origin将合并在从origin远程(通常为master )设置为默认的任何分支中。 In such cases, git merge origin and git merge origin/master will do the same thing. 在这种情况下, git merge origingit merge origin/master将做同样的事情。 If you wanted to merge in a different branch from origin , though, you'd have to specify the branch. 但是,如果要在origin的不同分支中合并,则必须指定分支。

The argument(s) to merge are resolved to commit-IDs. merge的参数merge解析为commit-ID。 This means that the rules in gitrevisions are applied. 这意味着应用了gitrevisions中的规则。 In general, origin/ name resolves to one of the "remote branchs" that git fetch and git push keep up-to-date on every fetch-and-push. 通常, origin/ name解析为git fetchgit push之一的“远程分支”在每次获取和git push保持最新。

A "remote branch", also called a "remote-tracking branch", is simply a branch-like label whose "full name" starts with refs/remotes/ . “远程分支”,也称为“远程跟踪分支”,只是一个类似分支的标签,其“全名”以refs/remotes/开头。 All the ones for the remote named origin are in refs/remotes/origin/ . 远程命名origin都在refs/remotes/origin/ In normal operation git fetch consults some remote (like origin ) git repository and asks it: "hey, what branches do you have over there, and what are their SHA-1 values?" 在正常操作中, git fetch查询一些远程(如origin )git存储库并询问:“嘿,你在那边有什么分支,以及它们的SHA-1值是什么?” When it gets the answers, it stores them locally, in your git repository: refs/remotes/origin/master , refs/remotes/origin/devel , and so on. 当它得到答案时,它会将它们存储在你的 git存储库中: refs/remotes/origin/masterrefs/remotes/origin/devel等等。 So that lets you know what things looked like "over there", the last time your git had a chance to sync up. 这样可以让你知道“那边”是什么样的东西,你的git最后一次有机会同步。

These should not be confused with what git calls "tracking branches" (or "local tracking branches"). 这些不应该与git所谓的“跟踪分支”(或“本地跟踪分支”)相混淆。 Local branches are labels whose "full name" starts with refs/heads/ . 本地分支是“全名”以refs/heads/开头的标签。 They're considered "tracking" if they have "upstream" information associated with them. 如果他们有与他们相关的“上游”信息,他们被认为是“跟踪”。 You can have the upstream information put in when you first create the branch—this is typically the case for local branches with the "same" names as remote-branches, like master vs origin/master —or you can set (or change) it later with git branch --set-upstream-to . 您可以在首次创建分支时输入上游信息 - 这通常是具有与远程分支“相同”名称的本地分支的情况,例如master vs origin/master - 或者您可以设置(或更改)它稍后使用git branch --set-upstream-to

All of these are different from the git pull syntax: git pull origin master is rather different, and much older, predating the whole idea of "remote-tracking branches" entirely. 所有这些都与git pull语法不同: git pull origin master是相当不同的,并且更古老,完全超越了“远程跟踪分支”的整个想法。 1 This sort of accidental resemblance, as it were, I think is the source of a whole lot of confusion (I know I found it confusing, years ago). 1这种偶然的相似之处,我认为是很多混乱的根源(我知道多年前我发现它很混乱)。


1 Specifically, you used to (and still can) git pull from a URL rather than a "remote" name. 1具体来说,您曾经(并且仍然可以)从URL而不是“远程”名称进行git pull That went (and still goes) over to the other git repository as usual, and gets a list of at least some of its branches (sometimes just the one of interest). 这通常会(并且仍然)转移到另一个git存储库,并获得至少一些分支的列表(有时只是感兴趣的一个)。 But, because those are its branches, not yours, they are then recorded in a file called FETCH_HEAD . 但是,因为它们是它的分支而不是你的分支,所以它们被记录在名为FETCH_HEAD的文件中。 So at this point, it will have brought master over from "over there", but put it into FETCH_HEAD rather than a remote-tracking branch—with a raw URL, you don't have a remote name, so there's no way to name the remote-tracking branch: there's no origin with which to construct the prefix refs/remotes/origin . 所以在这一点上,它会从“那里”带来master ,但把它放到FETCH_HEAD而不是远程跟踪分支 - 用原始URL,你没有远程名称,所以没有办法命名远程跟踪分支:有没有origin与构建前缀refs/remotes/origin The master argument to git-pull then means: "go search through FETCH_HEAD to find their master branch." 然后git-pullmaster参数意味着:“通过FETCH_HEAD搜索以找到它们的master分支。”

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

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