How can I make git-status check two different remotes?

前端 未结 3 1328
耶瑟儿~
耶瑟儿~ 2021-01-01 17:13

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 director         


        
相关标签:
3条回答
  • 2021-01-01 17:44

    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.

    0 讨论(0)
  • 2021-01-01 17:48

    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
    
    0 讨论(0)
  • 2021-01-01 18:02

    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 $2 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?)

    0 讨论(0)
提交回复
热议问题