显示所有分支的 git 前后信息,包括遥控器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7773939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:05:01  来源:igfitidea点击:

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

git

提问by kortina

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 to master.

在 github 项目中,您可以转到 /branches 页面并设置像这样的漂亮图表,每个分支显示每个分支相对于 master 的落后程度和领先程度。

git branch ahead behind

git 分支在前面

Is there a command line tool that does something similar? Something that works with remotes as well? For example,

是否有执行类似操作的命令行工具?也适用于遥控器的东西?例如,

git branch -v -v

is close to what I am looking for, but only works for local branches.

接近我正在寻找的,但仅适用于本地分支机构。

回答by Jehiah

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

我也对这个很好奇,所以我刚刚git branch-status编写了一个脚本,使用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

回答by VonC

Update 2015

2015 年更新

My initial answer below is not ideal as the upstream branch is not necessarily the branch you are pushing to. It is only the branch you are pulling from.

我在下面的初始答案并不理想,因为上游分支不一定是您要推送的分支。这只是您要从中拉出的分支。

With Git 2.5+, the correct command is:

使用 Git 2.5+,正确的命令是:

git for-each-ref --format="%(refname:short) %(upstream:track)" refs/heads

See more at "Viewing Unpushed Git Commits".

在“查看未推送的 Git 提交”中查看更多信息。

(as pointed out by void.pointerin the comments, upstream:trackis more precise than push:track, depending on the default push policy)

(如所指出的void.pointer意见upstream:track是不是更精确push:track,这取决于默认推送策略



Git 2.13 (Q2 2017) uses a more generic ref-filter API with a more complete git for-each-ref push:

Git 2.13(2017 年第二季度)使用更通用的 ref-filter API 和更完整的git for-each-ref push

See commit 3d9e4ce, commit 949af06, commit 56b4360, commit 6eac70f, commit 1a34728, commit 1a0ca5e, commit 3a42980, commit 17938f1, commit 3ba308c, commit a798410, commit b180e6f, commit 01f9582, commit 7743fcc, commit ffd921d, commit 99c6a71, commit d4919bb, commit 42d0eb0, commit 4f3e3b3, commit c58fc85(10 Jan 2017) by Karthik Nayak (KarthikNayak).
(Merged by Junio C Hamano -- gitster--in commit 93e8cd8, 27 Feb 2017)

提交3d9e4ce提交949af06提交56b4360提交6eac70f提交1a34728提交1a0ca5e提交3a42980提交17938f1提交3ba308c提交a798410提交b180e6f提交01f9582提交7743fcc提交ffd921d提交99c6a71提交d4919bb提交42d0eb0提交 4f3e3b3提交 c58fc85(2017 年 1 月 10 日)由Karthik Nayak (KarthikNayak)
(由Junio C gitsterHamano合并-- --提交 93e8cd8,2017 年 2 月 27 日)

push:

The name of a local ref which represents the @{push}location for the displayed ref.
Respects :short, :lstrip, :rstrip, :track, and :trackshortoptions as upstreamdoes.
Produces an empty string if no @{push}ref is configured.

If lstrip=<N>(rstrip=<N>) is appended, strips <N>slash-separated path components from the front (back) of the refname
(e.g. %(refname:lstrip=2)turns refs/tags/foointo fooand %(refname:rstrip=2)turns refs/tags/foointo refs).

If <N>is a negative number, strip as many path components as necessary from the specified end to leave -<N>path components
(e.g. %(refname:lstrip=-2)turns refs/tags/foointo tags/fooand %(refname:rstrip=-1)turns refs/tags/foointo refs)

本地 ref 的名称,表示@{push}显示的 ref的位置。
尊重:short:lstrip:rstrip:track:trackshort选项upstream
如果没有@{push}配置 ref,则生成一个空字符串。

如果lstrip=<N>rstrip=<N>)将被附加,条<N>从refname的前部(背面)斜线分隔路径组件
(如%(refname:lstrip=2)refs/tags/foofoo%(refname:rstrip=2)匝数refs/tags/foorefs)。

如果<N>是一个负数,条之多路径组件从指定的结束必要离开-<N>路径组件
(如%(refname:lstrip=-2)refs/tags/footags/foo%(refname:rstrip=-1)匝数refs/tags/foorefs



Original answer (2014)

原始答案 (2014)

Another way will be available with Git 1.9/2/0 (Q1 2014).
See commit b28061cfrom Ramkumar Ramachandra (artagnon):

Git 1.9/2/0(2014 年第一季度)将提供另一种方式。
请参阅来自Ramkumar Ramachandra (artagnon) 的提交 b28061c

for-each-ref: introduce %(upstream:track[short])

for-each-ref: 介绍 %(upstream:track[short])

Introduce:

  • %(upstream:track)to display "[ahead M, behind N]" and
  • %(upstream:trackshort)to display "=", ">", "<", or "<>" appropriately (inspired by contrib/completion/git-prompt.sh).

Now you can use the following format in for-each-ref:

介绍:

  • %(upstream:track)显示“ [ahead M, behind N]”和
  • %(upstream:trackshort)适当地显示“ =”、“ >”、“ <”或“ <>”(受 启发contrib/completion/git-prompt.sh)。

现在您可以在 for-each-ref 中使用以下格式:

%(refname:short) %(upstream:trackshort)

to display refs with terse tracking information.

Note that :trackand :trackshortonly work with "upstream", and error out when used with anything else.

显示带有简洁跟踪信息的引用。

请注意,:trackand:trackshort只能与“ upstream”一起使用,与其他任何东西一起使用时会出错。