如何使git-status检查两个不同的远程仓库?

20

每个 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无关,只是一个方便的例子。)


3
当本地分支设置为跟踪远程分支时,使用git status命令会得到该输出结果。我不确定是否可以同时将本地分支设置为跟踪多个远程分支。请参阅git branch命令的--track选项文档 - user456814
似乎目前还不可能实现。有些空闲时间和C语言知识的人应该考虑添加这个功能,例如“git status -r other-remote”,并发送一个补丁。 - chrishiestand
1
类似的问题(以及类似的答案:不可能)可以在https://dev59.com/RG855IYBdhLWcg3wik-D找到,那里还有更多相关链接。 - Ben Creasy
3个回答

13

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。如果要使用任何分支而不进行硬编码,则可以修改上面的脚本以首先获取当前的远程+分支,然后还原它。


2
好的,该命令只是打印出与 git status 相同的内容,只不过打印了两次。 - Max Yankov
如果它能够为您提供“分别指定的两个远程状态”,那就太好了,但事实并非如此。请参阅https://www.kernel.org/pub/software/scm/git/docs/git-status.html上的文档。 - Ben Creasy
1
@BenCreasy 很好的观点。由于 git status 仅针对当前远程跟踪分支工作,因此我更改了示例以在请求状态之前更改跟踪分支。 - Brent Faust
“远程跟踪分支” - 但是每个远程都可以有多个远程跟踪分支,是吗? branch -a 可以很好地列出我的“remotes/<remotename>/master”分支。 - Sz.

12

git status 是你工作目录下的状态,以单分支的方式呈现。

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

git branch -avvv

1
截至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 $1 commits behind $upstream";;
    *)   echo "Your branch and $upstream have diverged,"
         echo "and have $2 and $1 different commits each, respectively.";;
    esac
}

(上面的输出旨在与git status的输出匹配,不适用于两个参数的形式,但它展示了如何在此处执行所需操作。)

(由于我如何进行git status upstream?的链接,在2020年回答)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接