简体   繁体   English

git status branch.upstream 和 branch.ab 的区别

[英]difference between git status branch.upstream and branch.ab

I'm just reading the git status docs (ver 2.34.0), and I found something like this:我只是在阅读git status文档(版本 2.34.0),我发现了这样的内容:

Line                                     Notes
------------------------------------------------------------
# branch.oid <commit> | (initial)        Current commit.
# branch.head <branch> | (detached)      Current branch.
# branch.upstream <upstream_branch>      If upstream is set.
# branch.ab +<ahead> -<behind>           If upstream is set and the commit is present.
------------------------------------------------------------

So theoretically, branch.upstream and branch.ab appear upon different conditions but from my tests, they always appear at the same time.所以理论上, branch.upstreambranch.ab出现在不同的条件下,但从我的测试来看,它们总是同时出现。
Can someone make a condition where only branch.upstream appears ?有人可以做出只出现branch.upstream的条件吗? Thanks!谢谢!

You may set an upstream to a branch:您可以将上游设置为分支:

git branch -u some/upstream

If yostate u choose an upstream which doesn't exist yet (for example: git branch -u origin/doesnt/exist/yet ), you should be in that "If upstream is set."如果 yostate 你选择了一个尚不存在的上游(例如: git branch -u origin/doesnt/exist/yet ),你应该在“如果上游已设置”。 without "... and the commit is present."没有“......并且提交存在。”


In practice, however, you will not often be this situation.然而,在实践中,你不会经常出现这种情况。 It may happen if a remote branch gets deleted by someone else, and one of your local branches is still set to track that remote branch.如果远程分支被其他人删除,并且您的本地分支之一仍设置为跟踪该远程分支,则可能会发生这种情况。

I'll expand a bit on my comment on LeGEC's answer here.我将在这里扩展我对 LeGEC 答案的评论。

Suppose you have a repository:假设您有一个存储库:

$ cd <somewhere-with-repository>

and that repository has a remote name, eg, origin , and the remote has some branch(es).并且该存储库具有远程名称,例如origin ,并且远程具有一些分支。 We now do your git status --porcelain=v2 to get an example output:我们现在执行您的git status --porcelain=v2以获取示例 output:

$ git status --porcelain=v2 -b
# branch.oid 2808ac68000c62c3db379d73e3b7df292e333a57
# branch.head master
# branch.upstream origin/master
# branch.ab +0 -0

This particular repository has only master and origin/master :这个特定的存储库只有masterorigin/master

$ git branch -a
* master
  remotes/origin/master

so now we need to create a new branch to trigger your desired condition:所以现在我们需要创建一个新分支来触发你想要的条件:

$ git switch -c foo
Switched to a new branch 'foo'
$ git status --porcelain=v2 -b
# branch.oid 2808ac68000c62c3db379d73e3b7df292e333a57
# branch.head foo

We now need to set the upstream to origin/foo , but git branch -u refuses to do it:我们现在需要将上游设置为origin/foo ,但git branch -u拒绝这样做:

$ git branch -u origin/foo
error: the requested upstream branch 'origin/foo' does not exist
hint: 
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run "git fetch" to retrieve it.
hint: 
hint: If you are planning to push out a new local branch that
hint: will track its remote counterpart, you may want to use
hint: "git push -u" to set the upstream config as you push.

But we can work around that using the underlying branch.foo.remote and branch.foo.merge settings:但是我们可以使用底层的branch.foo.remotebranch.foo.merge设置来解决这个问题:

$ git config branch.foo.remote origin
$ git config branch.foo.merge refs/heads/foo
# branch.oid 2808ac68000c62c3db379d73e3b7df292e333a57
# branch.head foo
# branch.upstream origin/foo

Using git branch -vv shows us this:使用git branch -vv向我们展示了这一点:

$ git branch -vv
* foo    2808ac6 [origin/foo: gone] add xheap, for a priority queue for py3k
  master 2808ac6 [origin/master] add xheap, for a priority queue for py3k

This is what we'd see as well with git push -u origin foo followed by a deletion of origin/foo that prunes our own refs/remotes/origin/foo .这也是我们在git push -u origin foo然后删除origin/foo修剪我们自己的refs/remotes/origin/foo foo 时看到的。

(It's a bit curious that git branch -u won't let you set up this situation directly, since it would obviate the need for git push -u later and hence be useful in scripts. But the lower-level git config commands do let you set up this situation.) git branch -u不会让你直接设置这种情况,因为它可以避免git push -u的需要,因此在脚本中很有用。但是较低级别的git config你设置了这种情况。)

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

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