Git 分支:master vs. origin/master vs. remotes/origin/master
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10588291/
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 branching: master vs. origin/master vs. remotes/origin/master
提问by John Rumpel
I think I'm on the right track to understand the basic concepts of git.
我认为我在理解 git 的基本概念方面走在正确的轨道上。
I've already set up and cloned a remote repository. I also created a server side empty repository, and linked my local repository to it.
我已经设置并克隆了一个远程存储库。我还创建了一个服务器端空存储库,并将我的本地存储库链接到它。
My problem is that I don't understand the difference between:
我的问题是我不明白以下之间的区别:
- origin/master vs. remotes/origin/master
- origin/master vs. remotes/origin/master
As far as I have understood, masteris a local branch, and remotes/origin/masteris a remote one.
据我了解,master是本地分支,而remotes/origin/master是远程分支。
But what exactly is origin/master?
但到底什么是origin/master?
回答by larsks
Take a clone of a remote repository and run git branch -a
(to show all the branches git knows about). It will probably look something like this:
获取远程存储库的克隆并运行git branch -a
(以显示 git 知道的所有分支)。它可能看起来像这样:
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
Here, master
is a branch in the local repository. remotes/origin/master
is a branch named master
on the remote named origin
. You can refer to this as either origin/master
, as in:
这里,master
是本地存储库中的一个分支。 remotes/origin/master
是名为master
远程命名的分支origin
。您可以将其称为 任一origin/master
,如下所示:
git diff origin/master..master
You can also refer to it as remotes/origin/master
:
您也可以将其称为remotes/origin/master
:
git diff remotes/origin/master..master
These are just two different ways of referring to the same thing (incidentally, both of these commands mean "show me the changes between the remote master
branch and my master
branch).
这只是指代同一事物的两种不同方式(顺便说一句,这两个命令的意思都是“向我展示远程master
分支和我的master
分支之间的变化)。
remotes/origin/HEAD
is the default branch
for the remote named origin
. This lets you simply say origin
instead of origin/master
.
remotes/origin/HEAD
是default branch
远程命名的origin
。这让您可以简单地说origin
而不是origin/master
。
回答by ErichBSchulz
Short answer for dummies like me (stolen from Torek):
像我这样的傻瓜的简短回答(从 Torek 那里偷来的):
- origin/masteris "where master was over there last time I checked"
- masteris "where master is over here based on what I have been doing"
- origin/master是“我上次检查时主人在那里的地方”
- master是“根据我一直在做的事情,master 在哪里”
回答by torek
Technically there aren't actually any "remote" things at all1in your Git repo, there are just local names that shouldcorrespond to the names on another, different repo. The ones named origin/whatever
will initially match up with those on the repo you cloned-from:
从技术上讲,您的 Git 存储库中实际上根本没有任何“远程”事物1,只有本地名称应该与另一个不同的存储库中的名称相对应。命名的origin/whatever
最初将与您从中克隆的存储库中的那些匹配:
git clone ssh://some.where.out.there/some/path/to/repo # or git://some.where...
makes a local copy of the other repo. Along the way it notes all the branches that were there, and the commits those refer-to, and sticks those into your local repo under the names refs/remotes/origin/
.
制作另一个 repo 的本地副本。一路上,它记录了那里的所有分支,以及那些引用的提交,并将它们粘贴到您的本地存储库中的名称下refs/remotes/origin/
。
Depending on how long you go before you git fetch
or equivalent to update "my copy of what's some.where.out.there", they may change their branches around, create new ones, and delete some. When you do your git fetch
(or git pull
which is really fetch plus merge), your repo will make copies of their new work and change all the refs/remotes/origin/<name>
entries as needed. It's that moment of fetch
ing that makes everything match up (well, that, and the initial clone, and some cases of push
ing too—basically whenever Git gets a chance to check—but see caveat below).
根据你在你之前走多长时间git fetch
或相当于更新“我的 some.where.out.there 副本”,他们可能会改变他们的分支,创建新的,并删除一些。当你做你的git fetch
(或者git pull
真正的获取加合并)时,你的仓库将复制他们的新工作并refs/remotes/origin/<name>
根据需要更改所有条目。正是fetch
ing的那一刻让一切都匹配起来(好吧,那个,还有最初的克隆,还有一些push
ing 的情况——基本上是每当 Git 有机会检查时——但请参阅下面的警告)。
Git normally has you refer to your own refs/heads/<name>
as just <name>
, and the remote ones as origin/<name>
, and it all just works because it's obvious which one is which. It's sometimes possible to create your own branch names that make it not obvious, but don't worry about that until it happens. :-) Just give Git the shortest name that makes it obvious, and it will go from there: origin/master
is "where master was over there last time I checked", and master
is "where master is over here based on what I have been doing". Run git fetch
to update Git on "where master is over there" as needed.
Git 通常让您将自己的refs/heads/<name>
称为 just <name>
,将远程的称为origin/<name>
,这一切都可以正常工作,因为很明显哪个是哪个。有时可以创建自己的分支名称使其不明显,但在它发生之前不要担心。:-) 只需给 Git 起一个显而易见的最短名称,它就会从那里开始:origin/master
是“我上次检查时主人在那里的地方”,以及master
“根据我一直在做的事情,主人在这里的地方” . 根据需要运行git fetch
以更新“主人在那里的位置”上的 Git。
Caveat: in versions of Git older than 1.8.4, git fetch
has some modes that don't update "where master is over there" (more precisely, modes that don't update any remote-tracking branches). Running git fetch origin
, or git fetch --all
, or even just git fetch
, doesupdate. Running git fetch origin master
doesn't. Unfortunately, this "doesn't update" mode is triggered by ordinary git pull
. (This is mainly just a minor annoyance and is fixed in Git 1.8.4 and later.)
警告:在 1.8.4 之前的 Git 版本中,git fetch
有一些模式不会更新“master 在那里”(更准确地说,不更新任何远程跟踪分支的模式)。运行git fetch origin
,或git fetch --all
,甚至只是git fetch
,确实更新。跑步git fetch origin master
没有。不幸的是,这种“不更新”模式是由普通的git pull
. (这主要只是一个小问题,在 Git 1.8.4 及更高版本中已修复。)
1Well, there is one thing that is calleda "remote". But that's also local! The name origin
is the thing Git calls "a remote". It's basically just a short name for the URL you used when you did the clone. It's also where the origin
in origin/master
comes from. The name origin/master
is called a remote-tracking branch, which sometimes gets shortened to "remote branch", especially in older or more informal documentation.
1嗯,有一种东西叫做“远程”。但这也是本地的!这个名字origin
是 Git 称之为“远程”的东西。它基本上只是您在进行克隆时使用的 URL 的简称。这也是origin
in 的origin/master
来源。该名称origin/master
称为远程跟踪分支,有时会缩写为“远程分支”,尤其是在较旧或更非正式的文档中。
回答by MKJ
I would try to make @ErichBSchulz's answer simpler for beginners:
我会尽量让@ErichBSchulz 的答案对初学者更简单:
- origin/masteris the state of master branch on remote repository
- masteris the state of master branch on local repository
- origin/master是远程仓库上 master 分支的状态
- master是本地仓库上 master 分支的状态
回答by Gnanasekar S
- origin- This is a custom and most common name to point to remote.
- origin- 这是指向远程的自定义和最常见的名称。
$ git remote add origin https://github.com/git/git.git
--- You will run this command to link your github project to origin. Here origin is user-defined.You can rename it by $ git remote rename old-name new-name
$ git remote add origin https://github.com/git/git.git
--- 您将运行此命令将您的 github 项目链接到 origin。这里的原点是用户定义的。你可以重命名它$ git remote rename old-name new-name
- master- The default branch name in Git is master. For both remote and local computer.
- master- Git 中的默认分支名称是 master。适用于远程和本地计算机。
- origin/master- This is just a pointer to refer master branch in remote repo. Remember i said origin points to remote.
- origin/master- 这只是一个指向远程仓库中 master 分支的指针。记得我说过原点指向远程。
$ git fetch origin
- Downloads objects and refs from remote repository to your local computer [origin/master]. That means it will not affect your local master branch unless you merge them using $ git merge origin/master
. Remember to checkout the correct branch where you need to merge before run this command
$ git fetch origin
- 将对象和引用从远程存储库下载到您的本地计算机 [origin/master]。这意味着它不会影响您的本地 master 分支,除非您使用$ git merge origin/master
. 请记住在运行此命令之前签出需要合并的正确分支
Note: Fetched content is represented as a remote branch. Fetch gives you a chance to review changes before integrating them into your copy of the project. To show changes between yours and remote $git diff master..origin/master
注意:获取的内容表示为远程分支。Fetch 让您有机会在将更改集成到您的项目副本之前查看更改。显示您的和遥控器之间的变化$git diff master..origin/master
回答by rick
One clarification (and a point that confused me):
一个澄清(以及让我困惑的一点):
"remotes/origin/HEAD is the default branch" is not really correct.
“remotes/origin/HEAD 是默认分支”并不正确。
remotes/origin/master was the default branch in the remote repository (last time you checked). HEAD is not a branch, it just points to a branch.
remotes/origin/master 是远程存储库中的默认分支(上次检查时)。HEAD 不是一个分支,它只是指向一个分支。
Think of HEAD as your working area. When you think of it this way then 'git checkout branchname' makes sense with respect to changing your working area files to be that of a particular branch. You "checkout" branch files into your working area. HEAD for all practical purposes is what is visible to you in your working area.
将 HEAD 视为您的工作区域。当您以这种方式思考时,“git checkout branchname”对于将您的工作区文件更改为特定分支的文件是有意义的。您将分支文件“签出”到您的工作区域。出于所有实际目的,HEAD 是您在工作区域中可见的内容。
回答by Elliptical view
I think this git slash notation is probably best understood by looking inside your .git
folder.
我认为这个 git 斜杠符号可能通过查看您的.git
文件夹来最好地理解。
For example, here is a somewhat abbreviated tree of my .git for the LibreOffice source base.
例如,这里是我用于 LibreOffice 源代码库的 .git 的一个稍微简化的树。
In linuxsudo apt-get install tree
is useful to view this.
In WindowsI think the tree
command might still work.
在linuxsudo apt-get install tree
中查看这个很有用。
在Windows 中,我认为该tree
命令可能仍然有效。
Scroll down and take a look at refs (aka 'references') near the bottom:
向下滚动并查看底部附近的参考文献(又名“参考文献”):
$ tree
.
├── branches
├── config
├── description
├── FETCH_HEAD
├── gitk.cache
├── HEAD
├── hooks
│?? ├── applypatch-msg.sample
...
├── index
├── info
│?? └── exclude
├── logs
│?? ├── HEAD
│?? └── refs
│?? ├── heads
│?? │?? ├── master
│?? │?? └── remotes
│?? │?? └── origin
│?? └── remotes
│?? └── origin
│?? ├── distro
│?? │?? ├── cib
│?? │?? │?? └── libreoffice-6-0
│?? │?? ├── collabora
│?? │?? │?? └── cp-6.0
│?? │?? └── lhm
│?? │?? └── libreoffice-5-2+backports
│?? ├── HEAD
│?? ├── libreoffice-6-2
│?? ├── master
│?? └── private
│?? └── mst
│?? └── sw_redlinehide_4a
├── objects
│?? ├── info
│?? └── pack
│?? ├── pack-b80087dc57e2b3315f449ca0f1aaa91987bf0c5e.idx
│?? ├── pack-b80087dc57e2b3315f449ca0f1aaa91987bf0c5e.pack
│?? ├── pack-eb4e6808029e712d8d9c2671accbbd98aaeb9a04.idx
│?? └── pack-eb4e6808029e712d8d9c2671accbbd98aaeb9a04.pack
├── ORIG_HEAD
├── packed-refs
└── refs
├── heads
│?? ├── master
│?? └── remotes
│?? └── origin
├── remotes
│?? └── origin
│?? ├── distro
│?? │?? ├── cib
│?? │?? │?? └── libreoffice-6-0
│?? │?? ├── collabora
│?? │?? │?? └── cp-6.0
│?? │?? └── lhm
│?? │?? └── libreoffice-5-2+backports
│?? ├── HEAD
│?? ├── libreoffice-6-2
│?? ├── master
│?? └── private
│?? └── mst
│?? └── sw_redlinehide_4a
└── tags
└── libreoffice-6-2-branch-point
32 directories, 45 files
It might have been less confusing if it was laid out like this, but it wasn't:
如果它是这样布局的,它可能不会那么令人困惑,但事实并非如此:
repositories (i.e. independent trees)
├──local
│ └──master
│
└──origin1
│ └──master
└──origin2
└──master
We have three basic types of references: heads, remotes, and tags.
我们有三种基本类型的引用:head、remotes和tags。
.git/refs/headsholds our local master.
.git/refs/remotescan hold a number of remotes, although at the moment we only have originin it.
.git/refs/tags(is discussed elsewhere).
.git/refs/ head保存我们本地的master。
.git/refs/ remotes可以保存许多遥控器,尽管目前我们只有origin在里面。
.git/refs/标签(在别处讨论)。
originthus, is our one and only remote. It holds origin/master.
因此,起源是我们唯一的偏远地区。它包含origin/master。
We find that we have 2 HEADS(pointers to current branches), one local, and one remote:
我们发现我们有 2 个HEADS(指向当前分支的指针),一个本地,一个远程:
$ cat .git/HEAD # local: HEAD -> master
ref: refs/heads/master
$ cat .git/refs/remotes/origin/HEAD # remote origin: HEAD -> master
ref: refs/remotes/origin/master
If you list your branches:
如果您列出您的分支机构:
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/aoo/aw080
remotes/origin/aoo/trunk
remotes/origin/distro/capgemini/cg-4.1
remotes/origin/distro/cib/libreoffice-5-0
remotes/origin/distro/cib/libreoffice-5-1
remotes/origin/distro/cib/libreoffice-5-2
...
- The first branch listed (master) is the only one which is not a remote. So in this case we have one local branch. This is where we'll start our own work from, for our own new branches and subsequent commits.
- 列出的第一个分支 ( master) 是唯一一个不是远程的分支。所以在这种情况下,我们有一个本地分支。我们将从这里开始我们自己的工作,用于我们自己的新分支和后续提交。
Next, you may have many remote tracking branches, and we do here. You know these are remote tracking branches because they are prefixed with 'remotes/'. The ones shown here are for the remote named origin.
接下来,您可能有许多远程跟踪分支,我们在这里做。您知道这些是远程跟踪分支,因为它们以“ remotes/”为前缀。此处显示的是远程命名源。
So the second line is origin's current branchpointer. Remotes/origin: HEAD --points to--> master. This shows that in the remote repository, the current branch is their branch named master, (not to be confused with our local branch named master).
The remaining branches aren't found in your .git/refs/ tree, but rather you'll find them in
.git/packed-refs
.
所以第二行是原点的当前分支指针。遥控器/来源:HEAD --指向--> master。这表明在远程存储库中,当前分支是名为master的分支(不要与我们名为master的本地分支混淆)。
其余的分支在您的 .git/refs/ 树中找不到,而是在 .git/refs/ 树中找到
.git/packed-refs
。
When we git fetchwe download changes from the remote repository, into our remote tracking repository.
当我们git fetch 时,我们将更改从远程存储库下载到我们的远程跟踪存储库中。
When we git mergewe merge the changes in this local, remote tracking repository into our working local branch or branches, in this case into our master branch.
当我们git merge 时,我们将这个本地远程跟踪存储库中的更改合并到我们工作的本地分支或多个分支中,在这种情况下合并到我们的主分支。
(When we git pullwe do both of these two steps in one operation.)
(当我们git pull 时,我们在一个操作中完成这两个步骤。)
It's also interesting to note these localand remoteUUIDs for mastercurrently point to the same node (aka 'commit'):
值得注意的是,master 的这些本地和远程UUID当前指向同一个节点(又名“提交”):
$ cat refs/heads/master # local master
1ca409292272632f443733450313de5a82c54a9c
$ cat refs/remotes/origin/master # remote origin master
1ca409292272632f443733450313de5a82c54a9c
So our local master points to the same place as the remote's origin master:
所以我们的本地 master 和远程的 origin master 指向同一个地方:
[local] master = [remote] origin master
Finally, I think it's also useful to take a look at .git/packed-refs
最后,我觉得看看也有用 .git/packed-refs
$ cat packed-refs
# pack-refs with: peeled fully-peeled
3c1d4742e649fe9c8aed8c2817fe3e1f3364f298 refs/remotes/origin/aoo/aw080
e87c8b7922e9a73e0abb7f9a7a47c9ac3374a826 refs/remotes/origin/aoo/trunk
b70fdffb041c12f124dcc0822b61bf3450e53137 refs/remotes/origin/distro/capgemini/cg-4.1
5dbc3f1754809b9489faaf380b1a4bdbcfbb6205 refs/remotes/origin/distro/cib/libreoffice-5-0
cfdbc96ca47d68d6785fd21829a8d61f49d6e591 refs/remotes/origin/distro/cib/libreoffice-5-1
5189c8c47461ef09739086e55512fc6a10245273 refs/remotes/origin/distro/cib/libreoffice-5-2
3bee5917569ca8e6ee3b086458f5b1a917b88ca1 refs/remotes/origin/distro/cib/libreoffice-5-3
92fbe703f9ca480d3a2b8610d87e991c729edf77 refs/remotes/origin/distro/cib/libreoffice-5-4
05c0a5df66cc69d75280f05b804cf82f3387d42b refs/remotes/origin/distro/cib/libreoffice-6-0
7fe193e759b24b90852e6e327115b77114d7b119 refs/remotes/origin/distro/cib/libreoffice-6-1
8187f7aa413e7ef7b377eea2b057d336bf256867 refs/remotes/origin/distro/collabora/cd-5.3
7a6b608591e21ef61dc05cff9fc58da531035755 refs/remotes/origin/distro/collabora/cd-5.3-3.1
....
No doubt this leaves more questions than answers, but I think it can start to help you answer your own questions about what's what.
毫无疑问,这留下的问题多于答案,但我认为它可以开始帮助您回答关于什么是什么的问题。