如何让git-status检查两个不同的遥控器?

时间:2014-05-04 11:46:15

标签: git git-status

每个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个原点。 / p>

如何添加其他遥控器以便我看到这样的东西?

> 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 个答案:

答案 0 :(得分:5)

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

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

git branch -avvv

答案 1 :(得分:4)

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 :(得分:0)

至少从Git 2.28开始,简短的回答是“否”。与Brent Faust wrote一样,您必须设置当前分支的上游,然后运行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
}

(上面的输出旨在与git status的输出匹配,并且实际上不适用于两个参数的形式,但是它显示了如何执行此处可能要执行的操作。)< / p>


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