如何仅显示来自“git status”的分支过期信息?

问题描述

通常 git status 显示

  1. 当前分支名称(或分离的 HEAD 状态)
  2. 上游分支(如果有)是否领先、落后或分叉
  3. 已更改暂存以提交
  4. 未为提交而暂存的更改
  5. 未跟踪的文件

如何在没有文件信息的情况下只查看 1 和 2? git status 的选项通常会影响文件显示,试图缩短它通常会完全删除分支状态摘要

我希望看到这样的事情:

On branch mybranch
Your branch is behind 'origin/mybranch' by 7 commits,and can be fast-forwarded.
  (use "git pull" to update your local branch)

没有别的。

是否有一些 git 命令专门显示此信息?

解决方法

# Branch name or commit hash if detached HEAD
git symbolic-ref --quiet --short HEAD 2> /dev/null || \
    git rev-parse --short HEAD 2> /dev/null

# Check if an upstream branch is configured for the current branch
up=`git rev-parse --abbrev-ref @{u} 2>/dev/null`
if [ -n "$up" -a "$up" != "@{u}" ]; then
    # Count the number of commits ahead/behind the upstream
    git rev-list --count --left-right @{u}...HEAD
fi

您可以从中创建 git 别名或 shell 脚本。