首页 > 解决方案 > 在 `git status` 中显示 @{push} 和 @{upstream} 的状态

问题描述

对于拉取请求,我有:

git status很好地显示了@{upstream}增量,例如:Your branch is up to date with 'up/master'.

但我真正想要的是同时看到@{upstream}@{push}信息。

我可以手动添加它,但有更好的方法吗?

标签: git

解决方案


我有以下status别名(我运行git s而不是git status已经运行)~/.config/git/config

[alias]
  s   = "!git status && git push-status #" # Status including delta from @{push}.

  # Show the status of this branch relative to @{push}.
  push-status = "! push_branch=$(git push-branch 2>/dev/null) || { echo \"No push branch set.\" && exit 0; } \
    ; both=$(git rev-list --left-right --count HEAD...@{push}) \
    ; [[ $both == '0    0' ]] && echo \"Your branch is up to date with push branch $push_branch.\" \
    || echo \"Your branch is ${both%%   *} commit(s) ahead and ${both##*    } commit(s) behind push branch $push_branch.\" #"

这将给出如下所示的输出:

On branch my-pr-branch
Your branch and 'up/master' have diverged,
and have 1 and 3 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

nothing to commit, working tree clean
Your branch is 2 commit(s) ahead and 1 commit(s) behind push branch fork/my-pr-branch.

它不如内置的东西好,但它可以工作。


包括在提示中

为了让它显示在您的提示中,您可能已经从上游获取了增量,您可以简单地通过以下方式对其进行扩充:

# Previous command to check upstream delta:
upstream_delta=$(command git rev-list --left-right --count 'HEAD...@{u}')
# New command to check push delta:
push_upstream_delta=$(command git rev-list --left-right --count 'HEAD...@{push}')

输出如下所示:(0 0两个数字用制表符分隔)。第一个数字是前面提交,第二个是后面提交。

如果您使用 ⇣⇡ 表示上游增量,您可能会喜欢 ⇠⇢ 表示推送增量。


推荐阅读