Home > Enterprise >  difference between git status branch.upstream and branch.ab
difference between git status branch.upstream and branch.ab

Time:11-23

I'm just reading the git status docs (ver 2.34.0), and I found something like this:

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.
Can someone make a condition where only branch.upstream appears? Thanks!

CodePudding user response:

I'll expand a bit on my comment on LeGEC's answer here.

Suppose you have a repository:

$ cd <somewhere-with-repository>

and that repository has a remote name, e.g., origin, and the remote has some branch(es). We now do your git status --porcelain=v2 to get an example 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:

$ 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:

$ 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:

$ 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
* 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.

(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.)

CodePudding user response:

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." without "... and the commit is present."

[edit] : actually (as noted by @torek), git branch -u some/upstream will check that some/upstream exists, and will not allow you to track a non existing branch.
"branch tracking" is stored in your repository's configuration, you can edit your configuration file to forcibly set a non existing remote branch :

[branch "test"]
    remote = origin
    merge = refs/heads/doesnt/exist

In practice, however, you will not often be this situation.

You will generally set branch tracking through commands such as git checkout <origin/somebranch> or git push -u ..., both of which make sure that the "remote branch" exists at the time the link is created or updated.

The situation may happen when a remote branch gets deleted by someone else, and one of your local branches is still set to track that remote branch.

  •  Tags:  
  • git
  • Related