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

8
当使用git log时,我可以提供--since=<date>来限制日志只显示比特定日期更晚的提交。
使用git branch -r,我可以获取所有远程分支。
如何获取包含比给定日期更新的提交的分支列表,即包含比我感兴趣的日期更新的所有分支?
或者,如果这很难或不可能,仅考虑分支末端的日期可能已经足够。

澄清一下:需要的是符合条件的分支列表。 - Martin
3个回答

2
如何获取具有早于给定日期的贡献的分支列表,即包含比我感兴趣的日期更新的提交的所有分支?
以下是一种可能的方法:使用 git for-each-ref 运行。
git log -1 --since=<date> <branch>

对于您的仓库中的每个分支引用。如果此git log命令的输出不为空,则相关分支包含比<date>更新的提交,您应该在列表中打印该分支的名称;否则,它不包含更新的提交,您不应该打印其名称。
以下是一个shell脚本,它接受一个参数,应该是Git可以识别为日期的字符串(例如2014/12/25 13:003.months.agoyesterday等),并列出所有“绿色分支”(缺乏更好的术语),即包含比指定日期更新的提交的本地分支。
#!/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"
$

这说明所有五个分支都包含了“昨天”提交的代码,但是没有包含“今天”的提交。

2

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

git log --oneline --decorate --branches --remotes --since=$date \
        --simplify-by-decoration

从那里开始,只需要进行格式化处理即可。

0

您可以使用以下代码显示some-branch的最新提交日期:

git log -1 --format=format:%cd some-branch

这个日期也可以用不同的格式打印出来,可以查看git log手册页面上的--date选项。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接