18

每个 git 用户都习惯这样:

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

nothing to commit, working directory clean

然而,最近我开始使用两个遥控器而不是一个(heroku 和 github,我认为这是非常标准的情况),并且它开始让我在git status输出中只看到 1 个来源而烦恼。

我怎样才能添加其他遥控器,这样我才能看到这样的东西?

> 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

(这个问题与heroku或github无关,只是一个方便的例子。)

4

3 回答 3

10

git status是您的工作树的状态,一次一个分支的状态。

如果您想查看所有分支状态,请执行

git branch -avvv
于 2014-05-04T15:30:25.257 回答
8

git status仅显示与远程跟踪分支的相对状态。但是临时更改远程跟踪分支很容易:

git branch -u <remote>/<branch>

然后git status将显示该分支的状态。

请注意,显示的更改是相同的,但正确显示了当前远程跟踪分支的前后提交次数。

获取所有远程分支状态的 bash 脚本:

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

要使用单个命令获取两个来源的状态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"

这会为您的 ~/.gitconfig 文件添加一个别名(您可以稍后对其进行编辑以更改主远程分支或命令s)。

请注意,origin/master被硬编码为默认分支。要在没有硬编码的情况下使用任何分支,可以修改上面的脚本以首先获取当前的远程+分支,然后恢复它。

于 2014-05-04T15:37:16.170 回答
0

至少从 Git 2.28 开始,简短的回答是“不”。正如Brent Faust 所写,如果要为多个上游值打印此信息,则必须设置当前分支的上游,然后运行git status,然后再次设置并再次运行。git status

一切都没有丢失

虽然你不能git status这样做,但你可以使用不同的 shell 命令来做你想做的事:

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

counts变量现在包含两个值:“远程领先,我落后”值和“远程落后,我领先”值。如果两个值都为零,则您的分支和选择的上游是偶数。(如果要交换计数,请交换$chosen_upstream$branch变量。)

把它变成一个更有用的 shell 函数(在普通旧的shbash两者中都有效):

# 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
}

(上面的输出旨在与 from 匹配,git status并不真正适合双参数形式,但它显示了如何在此处执行您可能想要执行的操作。)


(由于来自How do I do git status upstream?的链接,于 2020 年回答)

于 2020-08-27T03:35:56.803 回答