How do I show only branch out-of-dateness information from `git status`?

98 views Asked by At

Typically git status shows:

  1. Current branch name (or detached HEAD status)
  2. Whether upstream branch (if any) is ahead, behind or diverged
  3. Changed staged for commit
  4. Changes not staged for commit
  5. Untracked files

How do I view only 1 and 2 without information about files? git status's options typically affect display of files and trying to make it shorter typically removes branch status summary altogether.

I expect to see something like this:

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)

without anything else.

Is there some git command that shows specifically this info?

1

There are 1 answers

1
phd On
# 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

You can make a git alias or a shell script from that.