20

Every git user is accustomed to this:

> git status
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working directory clean

However, recently I've started working with two remotes instead of one (heroku and github, pretty standard situation, I think), and it started to annoy me to only see 1 origin in git status output.

How can I add other remote so I would see something like this?

> git status
On branch master
Your branch is up-to-date with 'origin/master'.
Your branch is up-to-date with 'heroku/master'.

nothing to commit, working directory clean

(This question has nothing to do with heroku or github, it's just a convenient example.)

Max Yankov
  • 12,551
  • 12
  • 67
  • 135
  • 3
    You get that output with `git status` because the local branch is set up to track the remote one. I'm not sure if you can set up a local branch to track more than one remote branch at a time. See the [`--track` option documentation for `git branch`](https://www.kernel.org/pub/software/scm/git/docs/git-branch.html). –  May 04 '14 at 15:21
  • Seems like this is currently not possible. Someone with some free time and knowledge of C should consider adding this functionality e.g. "git status -r other-remote" and sending a patch. – chrishiestand Jun 11 '16 at 02:11
  • 1
    Similar question (and similar answer: not possible) at http://stackoverflow.com/questions/4282486/how-to-track-more-than-one-remote-with-a-given-branch-using-git with a couple more linked there – Ben Creasy Jul 29 '16 at 16:53

3 Answers3

13

git status only shows the relative status to the remote tracking branch. But it's easy to change the remote tracking branch temporarily:

git branch -u <remote>/<branch>

Then git status will show the status of that branch.

Note that the changes displayed are the same, but the number of commits ahead/behind for the current remote tracking branch are properly displayed.

A bash script to get all remote branch status:

for o in $(git remote -v | grep fetch | cut -f 1 -); do # remote branch names
  git branch -u $o/master  # set remote tracking branch (git v1.8+ syntax)
  git status
  echo --------------------------------   # separator
  git branch -u origin/master >/dev/null  # restore original tracking branch
done

To get the status of both of your origins using the single command git s:

git config --global alias.s "for o in $(git remote -v | grep fetch | cut -f 1 -); do git branch -u $o/master; git status; echo; git branch -u origin/master >/dev/null; done"

This adds an alias to your ~/.gitconfig file (which you can later edit to change either the main remote branch or the command s).

Note that origin/master is hard-coded as the default branch. To work with any branch, without hard-coding, the script above could be modified to get the current remote+branch first, then restore it.

Brent Faust
  • 9,103
  • 6
  • 53
  • 57
  • 2
    Well, that command just prints out the same as `git status`, only two times – Max Yankov May 12 '14 at 13:36
  • It would be nice if it did give you "the status for both remotes (specified separately)", but it doesn't. See the docs at https://www.kernel.org/pub/software/scm/git/docs/git-status.html – Ben Creasy Jul 29 '16 at 16:45
  • 1
    @BenCreasy Good point. Since `git status` only works against the current remote tracking branch, I changed the example to change the tracking branch before asking for the status. – Brent Faust Aug 05 '16 at 21:52
  • "... *the* remote tracking branch" -- but there can be as many remote tracking branches as remotes, hmm? `branch -a` does list *both* or my "remotes//master" branches just fine. – Sz. Mar 14 '18 at 23:45
12

git status is the status of your worktree, one-branch-at-a-time status.

if you want to see all-branch status, do

git branch -avvv
jthill
  • 55,082
  • 5
  • 77
  • 137
1

The short answer, as of Git 2.28 at least, is "no". As Brent Faust wrote, you must set the upstream of the current branch, then run git status, then set it again and run it again, if you want git status to print this information for multiple upstream values.

All is not lost

While you can't get git status to do this, you can use a different shell command to do what you want:

counts=$(git rev-list --count --left-right $chosen_upstream...$branch)
# note: three dots

The counts variable now contains two values: the "remote ahead, me behind" value, and the "remote behind, me ahead" value. If both values are zero, your branch and the chosen upstream are even. (If you want the counts swapped, swap the $chosen_upstream and $branch variables.)

To turn this into a more-useful shell function (valid in plain old sh and bash both):

# report: invoke as report upstream [branch]
report() {
    local branch upstream count
    case $# in
    1) branch=$(git symbolic-ref HEAD 2>/dev/null) || return 1;;
    2) branch="$2";;
    *) echo "usage: report <upstream> [<branch>]" 1>&2; return 1;;
    esac
    upstream="$1"
    count=$(git rev-list --count --left-right "$upstream...$branch") || return 1
    set -- $count
    case $1,$2 in
    0,0) echo "Your branch is up-to-date with $upstream";;
    0,*) echo "Your branch is $2 commits ahead of $upstream";;
    *,0) echo "Your branch is $1 commits behind $upstream";;
    *)   echo "Your branch and $upstream have diverged,"
         echo "and have $2 and $1 different commits each, respectively.";;
    esac
}

(The output from the above is designed to match that from git status, and isn't really appropriate for the two-argument form, but it shows how to do what you might want to do here.)


(answered in 2020 due to link from How do I do git status upstream?)

torek
  • 448,244
  • 59
  • 642
  • 775