Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we run git status for a specific remote?

Tags:

git

git-remote

We have two remotes for our local git repository. One remote is called dev, the other one is called origin. When we run git status or git status dev, we receive this message:

# On branch 1.8.x
# Your branch is ahead of 'origin/1.8.x' by 4 commits.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean

The message tells us the status for the origin remote. That isn't what we want. How can we check the status for only the dev remote?

like image 920
Shaun Luttin Avatar asked Aug 13 '14 03:08

Shaun Luttin


1 Answers

Here's how git status gets that information.

  • What branch are we on?

    $ git symbolic-ref --short HEAD
    1.8.x
    

    (the above is the preferred method of finding out, generally; it errors-out if you're in "detached HEAD" mode), or:

    $ git rev-parse --symbolic-full-name HEAD
    refs/heads/1.8.x
    

    (this also works but can only get you the "full name": remove refs/heads/ to get the branch name).

  • What branch is "upstream" of 1.8.x?

    $ git rev-parse --symbolic-full-name '@{upstream}'
    refs/remotes/origin/1.8.x
    

    (the above is the easier way, but again gives you only the "full name" form; this time you have to remove refs/remotes/ to shorten it), or:

    $ git config --get branch.1.8.x.remote
    origin
    $ git config --get branch.1.8.x.merge
    refs/heads/1.8.x
    $ git config --get remote.origin.fetch
    +refs/heads/*:refs/remotes/origin/*
    

    (this is how git figures out the symbolic-full-name of @{upstream}—it's a bit complicated as you have to apply the branch-name mapping from the fetch line1 here).

  • How many revisions are there on one side or the other?

    $ git rev-list --count origin/1.8.x..1.8.x
    4
    $ git rev-list --count 1.8.x..origin/1.8.x
    0
    

    That is, we ask git rev-list to count how many commits are reachable from reference 1.8.x (your current branch) but not origin/1.8.x (your upstream). The answer is 4, so you're "ahead 4". Then we ask git rev-list to count how many commits are reachable from origin/1.8.x but not from 1.8.x. The answer is 0, so you're "behind 0", which git status can then be quiet about.

The git status command does all this work for whichever branch is set as the current branch's "upstream". So you can use git branch --set-upstream-to to change the upstream—in this case, changing it from origin/1.8.x to dev/1.8.x—and from then on you'll get that information, and only that information, from git status. (Change it back to get the information to switch back.)


If you want to get it temporarily, without switching branches, you need to do more or less "what git status does" but with an alternative "upstream", in this case, dev/1.8.x. It's not that difficult to write a shell script that does this, especially if you simply assume that branch B maps to branch rmt/B for the given remote rmt (then you need not do all the complicated mapping). That is, if you're on branch zog, and you're asking about remote bob, you just do the two rev-lists for bob/zog..zog and zog..bob/zog.


1Actually, you'd really want git config --get-all remote.origin.fetch in case there are multiple ref-mapping fetch lines. But this ref-mapping is a pain; probably there should be a git plumbing command to do it for you.

like image 141
torek Avatar answered Oct 07 '22 19:10

torek