我如何获得贡献比给定日期更早的分支列表,即所有包含比我感兴趣的日期更新的提交的分支?
这是一种可能的方法:使用git for-each-ref 运行
git log -1 --since=<date> <branch>
对于您的 repo 中的每个分支引用。如果这个git log 命令的输出是非空的,那么有问题的分支包含比<date> 更新的提交,你应该在你的列表中打印分支的名称;否则,它不会,你不应该打印它的名字。
这是一个带有一个参数的shell脚本,它应该是一个Git可以识别为日期的字符串(例如2014/12/25 13:00、3.months.ago、yesterday等),并列出所有“绿色分支” "(因为缺少更好的术语),即包含比指定日期更新的提交的本地分支。
#!/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 项目 repo 的克隆中进行测试
$ 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"
$
这表明所有五个分支都包含“昨天”的提交,但没有一个包含“今天”的提交。