至少从 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 函数(在普通旧的sh
和bash
两者中都有效):
# 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 年回答)