如何列出包含比特定日期更新的提交的所有分支?

时间:2015-01-30 14:27:27

标签: git git-branch git-log

使用git log时,我可以提供--since=<date>来限制日志提交比特定日期更新。

使用git branch -r,我可以获得所有远程分支。

我如何获得贡献小于给定日期的分支列表,即包含比我感兴趣的日期更新的提交的所有分支?

或者,如果这很难或不可能,只考虑分支小费的日期就足够了。

3 个答案:

答案 0 :(得分:2)

--simplify-by-decoration仅列出具有直接引用的提交:

git log --oneline --decorate --branches --remotes --since=$date \
        --simplify-by-decoration
从那里开始,这只是格式化问题。

答案 1 :(得分:1)

  

我如何获得贡献小于给定日期的分支列表,即所有包含比我感兴趣的日期更新的提交的分支?

以下是一种可能的方法:使用git for-each-ref来运行

git log -1 --since=<date> <branch>

您的仓库中的每个分支参考。如果此git log命令的输出非空,则有问题的分支包含比<date>更新的提交,您应该在列表中打印分支的名称;否则,它没有,你不应该打印它的名字。

这是一个shell脚本,它接受一个参数,该参数应该是Git可以识别为日期的字符串(例如2014/12/25 13:003.months.agoyesterday等),以及列出所有&#34;绿色分支&#34; (缺少更好的术语),即包含比指定日期更新的提交的本地分支。

#!/bin/sh

# git-greenbranch.sh
#
# List the local branches that contain commits newer than a specific date
#
# Usage: git greenbranch <date>
#
# To make a Git alias called 'greenbranch' out of this script,
# put the latter on your search path, and run
#
#   git config --global alias.greenbranch '!sh git-greenbranch.sh'

if [ $# -ne 1 ]
then
    printf "usage: git greenbranch <date>\n\n"
    printf "For more details on the allowed formats for <date>, see the\n"
    printf "'git-log' man page.\n"
    exit 1
fi

testdate=$1

git for-each-ref --format='%(refname:short)' refs/heads/ \
    | while read ref; do
          if [ -n "$(git rev-list --max-count=1 --since="$testdate" $ref)" ]
          then
              printf "%s\n" "$ref"
          fi
      done

exit $?

(该脚本可在GitHub上的Jubobs/git-aliases获得。)

为方便起见,您可以在用户级别定义运行相关脚本的Git别名(此处称为greenbranch.sh):

git config --global alias.greenbranch '!sh git-greenbranch.sh'

确保shell脚本在您的路径上。

在Git项目仓库的克隆中进行测试

$ git clone https://github.com/git/git/
# go grab a cup o' coffee...

$ cd git

# check all remote branches out (for testing purposes)
$ git checkout -b maint origin/maint
$ git checkout -b next origin/next
$ git checkout -b pu origin/pu
$ git checkout -b todo origin/todo

$ git greenbranch "yesterday"
maint
master
next
pu
todo
$ git greenbranch "today"
$

这表明所有五个分支都包含#34;昨天和#34;但没有包含提交的提交&#34;今天&#34;。

答案 2 :(得分:0)

您可以使用

some-branch上显示最近一次提交的日期
git log -1 --format=format:%cd some-branch

此日期也可以采用其他格式打印,请参阅--date手册页上的git log选项。

相关问题