Show git ahead and behind info for all branches, including remotes

前端 未结 2 1312
萌比男神i
萌比男神i 2020-11-27 03:14

On a github project you can go to a /branches page and se pretty graphs like this one that for each branch show how far behind and how far ahead each branch is with respect

2条回答
  •  借酒劲吻你
    2020-11-27 03:45

    I've been curious about this as well, so i just whipped up a git branch-status script that gives this information using git for-each-ref

    #!/bin/bash
    # by http://github.com/jehiah
    # this prints out some branch status (similar to the '... ahead' info you get from git status)
    
    # example:
    # $ git branch-status
    # dns_check (ahead 1) | (behind 112) origin/master
    # master (ahead 2) | (behind 0) origin/master
    
    git for-each-ref --format="%(refname:short) %(upstream:short)" refs/heads | \
    while read local remote
    do
        [ -z "$remote" ] && continue
        git rev-list --left-right ${local}...${remote} -- 2>/dev/null >/tmp/git_upstream_status_delta || continue
        LEFT_AHEAD=$(grep -c '^<' /tmp/git_upstream_status_delta)
        RIGHT_AHEAD=$(grep -c '^>' /tmp/git_upstream_status_delta)
        echo "$local (ahead $LEFT_AHEAD) | (behind $RIGHT_AHEAD) $remote"
    done
    

    Usage:

    $ git branch-status
    dns_check (ahead 1) | (behind 112) origin/master
    master (ahead 2) | (behind 0) origin/master
    

提交回复
热议问题