git:我如何看”远程分支?
在Azure上,我创建了一个基于主分支的新分支。
然后,我从桌面上跑了:
git fetch
我希望在运行时看到远程分支:
git branch -l
...但是我没有。然后,我尝试:
git branch -r
...查看远程分支。我的分支仍未列出。
我真的不明白为什么以上命令不起作用,我应该使用什么命令?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
git branch 非常严重,以至于它在很大程度上失去了所有含义。 就像是在一个每个人都被命名为bruce 的聚会上。我们必须对这个词非常重要。
现在,关于git的问题是,git 存储库在其核心上是 consits 的集合。提交是Git的全部内容。它们被编号,每个都有两件事:
这些数字是大的,丑陋的,随机的事物( hash IDS 或 oids ),这是人类不可能明智地记住或处理的。因此,存储库还带有 name 数据库。这意味着可以将存储库视为两个数据库。两者都很简单 key-value商店 “对象数据库”,由哈希ID找到,名称在“名称数据库”中,并以其名称找到。查找名称时获得的价值是哈希ID;查找提交哈希ID获得git git;这就是您获得提交的方式。
名称数据库中的名称包括分支名称,标签名称和其他此类名称。这些不是分支,除非通过 branch ,否则您的意思是这些的东西,然后它们是是分支。因此,单词 branch 本身是指这些,除非并不意味着这些。 (这就是为什么我建议您谨慎使用 branch 。)
因此,随着暂时的混乱,让我们专注于原始的哈希ID。当您拥有两个两个存储库时,这两个存储库可以相互连接。然后,他们可以共享他们的提交(使用
git fetch
和git push
例如),当他们这样做时,他们都将使用共享提交的相同哈希ID 。 不同的 consits 必须具有不同的哈希ID。 相同的提交一旦通过两个单独的git存储库共享,必须具有相同的哈希ID。这一切都意味着任何给定提交的哈希ID不仅大且丑陋,而且它也是一个特定的commit 所独有的。如果您进行 new 提交,您的git软件必须给出该git a unique hash ID,以前从未使用过,并且在任何git存储库中都不会再用, 曾经。如果您将您的git软件发送,该对某些其他其他 git存储库进行了一些其他 git软件,其他git软件必须使用相同的哈希ID 您的 git只是给您的新提交。
因此,重要的是 hash ids 。由两个不同的Git软件实现管理的两个Git存储库,它们相互连接,并通过RAW HASH ID连接 COMPTINC 。
但这是问题:人类无法处理原始哈希ID。我们喜欢名字!我们认为
main
和dev
和功能/简短
敏锐。但是哪个提交是main
?好吧,我的main
是我的最新提交。 您的main
是您的最新提交。如果我们俩都做了新提交,我的main
和您的main
必须转换为不同哈希ID 。结果是,没有GIT存储库可以信任任何其他其他 git存储库的名称。换句话说,每个存储库都有自己的私人名称数据库。但是我可以将我的git软件连接到您的git软件, get 您的consits 从中,当我这样做时,我想知道哪个提交订单hash iD代表您的最新
main
提交。我的git将为我存储它,但是它不会使用名称
main
来做到这一点。取而代之的是,我将使用git远程添加
:例如。我可以使用任何名称,而不仅仅是
Origin
,以及对我有用的任何URL,因此这只是一个示例。但是,如果我使用git克隆
复制您的所有提交,我的git软件将自动保存在名称onection 的情况下。 /code>在创建新存储库后,通过执行此
git远程添加
。因此,Origin
在这里是一个相当通用的名称。无论如何,在我的git软件连接到您的软件之后,我将获得您对我的所有新提交 - 我们俩都已经拥有的任何人都知道了,我的git知道了这一点,因为您和我在此处具有相同的 hash ids - 我的git软件会将它们保存在我的对象数据库中。然后,我的git软件将您的分支名称和更改将它们纳入我的远程跟踪名称:您的您的
main
将成为我的onement/main
,您的dev
将成为我的onect> onect/dev
,依此类推。基本上,我的git在您的 branch 名称的前面粘贴了我的远程名称,origin
。因此,一旦完成了这一切,我将获得您的最新提交,而我的git将使您的分支名称变成我的远程跟踪名称。要使我的git 列表我的远程跟踪名称,我运行
git branch -r
。这将它们列出为,例如,例如Origin/Main
。或者,我使用git branch -a
,它的git列表列出了我的 branch 名称和我的远程跟踪名称; 没有明显的原因,git现在将我的远程跟踪名称列为远程/Origin/main
等。由于 必须运行
git提取
。那是因为我的的克隆您的存储库只是坐在我的笔记本电脑上,直到我实际运行git git fetch
。我的git没有理由一直与您的git联系:我在 my clone或至少从开始时,我rangit提取
。我们所做的就是将CONTIST 添加到存储库中,因此,当您添加新提交时,我必须运行git fet
才能获得这些新提交。如果您的Azure设置尚存 ene 克隆,例如,如果在github上有一个git存储库,然后您有了azure clone将git存储库与azure git库,并且您在该名称中制作了一个新的分支名称。 Azure克隆 - 嗯,这不会影响 github 克隆。 GitHub克隆是其自己的存储库,其设备具有自己的分支名称。您必须让GitHub克隆从Azure存储库中获取 - 除了GitHub不会 Fetch 。相反,您必须将Azure克隆到推送到GitHub存储库。
推动就像提取一样,它传递了新提交,但与提取不同。也就是说,如果我有自己的一对存储库,一个在笔记本电脑上,一台在Github上,我在笔记本电脑上做了一个新的提交:
我现在必须将我的新提交发送给我的github克隆,使用
git push
我刚刚做出的最新提交是我想发送的。我的Git软件调用GitHub的Git软件,最终连接到GitHub上的存储库,而我的软件将转移到我最新提交的哈希ID上。 GitHub软件认为它没有该提交 - 在GitHub存储库中没有该哈希ID,因此他们从我的git中获取了这一提交。但是现在发生了不同的事情:
main
以记录此最新提交。main
。结果是(在这种情况下)我进行了github更新,他们的
main
branch-name。如果我在我的存储库中制作一个全新的 name :我必须运行
git push
,以便我要求github(请,如果可以的话)创建新名称newname
over there:This time they'll have the latest commit, so the send new commits phase of this whole operation does nothing.但是,如果可以,请在GitHub存储库中创建新的分支 name ,请创建或更新名称阶段。
请注意,我不必在github克隆中使用的笔记本电脑克隆中使用相同的名称。每个存储库都有自己的名称数据库,如果我愿意,我可以创建一个新的分支名称
hello
,然后让github create而是“ nameworld 。我通常不想要 这样做,因为它使事情变得复杂。我们微弱的人类大脑无法处理哈希ID,并且在双方使用不同的名称方面做得不好;我们只想称呼布鲁斯的一切,避免混乱。
Git overuses the word branch so heavily that it largely loses all meaning. It's like being at a party where everyone is named Bruce. We have to be super-careful with this word.
Now, the thing to know about Git is that a Git repository is, at its heart, a collection of commits. The commits are what Git is all about. They're numbered, and each one has two things:
The numbers are big, ugly, random-looking things (hash IDs or OIDs) that are impossible for humans to remember or deal with sensibly. So a repository also comes with a names database. That means a repository can be viewed as two databases. Both are simple key-value stores, with the commits being in the "object database" and found by hash IDs, and the names being in the "names database" and found by their name. The value you get when you look up a name is a hash ID; looking up a commit hash ID gets Git the commit; and that's how you get a commit.
The names in the names database include branch names, tag names, and other such names. These aren't branches, unless by branch you mean the things that these are, and then they are branches. So the word branch, by itself, means these, except when it doesn't mean these. (This is why I recommend being careful with the word branch.)
So, with that mess temporarily out of the way, let's just concentrate on the raw hash IDs. When you have two repositories, these two repositories can connect to each other. They can then share their commits (using
git fetch
andgit push
for instance), and when they do so, they will all use the same hash IDs for the shared commits. Different commits must have different hash IDs. The same commits, once shared across two separate Git repositories, must have the same hash IDs.What this all means is that the hash ID of any given commit is not just big and ugly, it's also unique to that one particular commit. If you make a new commit, your Git software has to give that Git a unique hash ID, never used before and never to be used again, anywhere, in any Git repository, ever. If you then have your Git software send that commit to some other Git software managing some other Git repository, that other Git software must use the same hash ID your Git just gave to your new commit.
So, it's the hash IDs that matter. Two Git repositories, managed by two different Git software implementations, connect to each other and exchange commits, by raw hash ID.
But here's the problem: humans can't deal with the raw hash IDs. We like names! We think
main
anddev
andfeature/short
are keen. But which commit ismain
? Well, mymain
is my latest commit. Yourmain
is your latest commit. If we've both made new commits, mymain
and yourmain
have to translate to different hash IDs.The result is that no Git repository can ever trust any other Git repository's names. Each repository has its own private names database, in other words. But I can connect my Git software to your Git software and get your commits from you, and when I do that, I would like to know which commit hash ID represents your latest
main
commit.My Git will store this for me, but it won't use the name
main
to do that. Instead, I'll define what Git calls a remote, usinggit remote add
:for instance. I can use any name, not just
origin
, and any URL that works for me, so this is just an example; but if I usegit clone
to copy all of your commits, my Git software will automatically save the URL under the nameorigin
by doing thisgit remote add
for me, after it creates a new repository. Soorigin
is a pretty common name here.Anyway, after my Git software connects to yours, I'll get all your commits that are new to me—any that we both already have, I already have, and my Git knows this because you and I have the same hash IDs here—and my Git software will save them away in my objects database. Then my Git software will take your branch names and change them into my remote-tracking names: your
main
will become myorigin/main
, yourdev
will become myorigin/dev
, and so on. Basically my Git sticks my remote name,origin
, and a slash, in front of your branch names.So once this is all done, I'll have your latest commits, and my Git will turn your branch names into my remote-tracking names. To have my Git list out my remote-tracking names, I run
git branch -r
. This lists them as, e.g.,origin/main
. Or, I usegit branch -a
, which has my Git list out my branch names and my remote-tracking names; for no obvious reason, Git now lists my remote-tracking names asremotes/origin/main
and so on.1For all this to work, though, I must run
git fetch
. That's because my clone of your repository just sits here on my laptop, until I actually do rungit fetch
. There's no reason for my Git to reach out to yours all the time: I have a full copy of every version of every file you've ever had in my clone, or at least, as of the last time I rangit fetch
. All we ever do is add commits to a repository, so when you add new commits, I have to rungit fetch
to obtain those new commits.If your Azure setup has yet another clone—if there's a Git repository over on GitHub, for instance, and then you had Azure clone that Git repository to Azure and you've made a new branch name in the Azure clone—well, that didn't affect the GitHub clone. The GitHub clone is its own repository, with its own set of branch names. You have to get the GitHub clone to fetch from the Azure repository—except that GitHub won't fetch. Instead, you'll have to get the Azure clone to push to the GitHub repository.
Pushing is like fetching in that it transfers new commits, but it's also unlike fetching. That is, if I have my own pair of repositories, one on my laptop and one on GitHub, and I make a new commit on my laptop:
I now have to send my new commit to my GitHub clone, using
git push
for instance:Here
origin
is the name of the remote, as before, andmain
is the name I use to identify the most recent commit I just made, which is the one I want to send. My Git software calls up GitHub's Git software and ends up connected to my repository on GitHub, and my software hands over the hash ID of my latest commit. The GitHub software sees that it does not have that commit—there's nothing in the GitHub repository with that hash ID—so they grab that commit from my Git. But now the different thing happens:main
in your repository to record this latest commit.main
.The result is that I've made GitHub update (in this case) their
main
branch-name. If I make a totally new name in my repository:I have to run
git push
such that I ask GitHub to (please, if it's OK) create the new namenewname
over there:This time they'll have the latest commit, so the send new commits phase of this whole operation does nothing. But the please if it's OK create or update a name phase creates the new branch name in the GitHub repository.
Note that I do not have to use the same names in my laptop clone that I use in my GitHub clone. Each repository has its own names database, and if I want, I can create a new branch name
hello
and then have GitHub create instead the nameworld
. I don't normally want to do that because it makes things complicated; our feeble human brains can't handle hash IDs and don't do well with using different names on the two sides; we want to just call everything Bruce, to avoid confusion. ????1They have a full name,
refs/remotes/origin/main
. Git normally strips off therefs/remotes/
part. Butgit branch -a
only strips off therefs/
part. Your own branch names also have a full name:main
is reallyrefs/heads/main
. The output fromgit branch
orgit branch -a
strips off therefs/heads/
part, always.There's a so-called plumbing command,
git for-each-ref
, meant for writing new Git commands or other scripts that do something useful, and it normally shows you every name, keeping the fullrefs/
stuff in the front. Its output is meant for a computer program to read, though, not for a human, so it's machine-friendly and kind of human-antagonistic.如果您链接了远程分支,则应使用
git remote -r
看到它们所有对我有用的分支机构,或者您可以使用Redrence链接在这里If you linked your remote branch you should see them by using
git remote -r
but if its not listed you can check remote origin for that and after that try push on your default branch because that should link your remote all branches that worked for me or you can use that redrence link here