【问题标题】:How can I list all branches that contain commits newer than a specific date?如何列出包含比特定日期更新的提交的所有分支?
【发布时间】:2015-03-30 01:16:43
【问题描述】:

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

有了git branch -r,我得到了所有的远程分支。

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

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

【问题讨论】:

  • 澄清:需要的是符合条件的分支列表。

标签: git git-branch git-log


【解决方案1】:

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

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

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

对于您的 repo 中的每个分支引用。如果这个git log 命令的输出是非空的,那么有问题的分支包含比&lt;date&gt; 更新的提交,你应该在你的列表中打印分支的名称;否则,它不会,你不应该打印它的名字。

这是一个带有一个参数的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 项目 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"
$

这表明所有五个分支都包含“昨天”的提交,但没有一个包含“今天”的提交。

【讨论】:

    【解决方案2】:

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

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

    从那里开始只是格式问题。

    【讨论】:

      【解决方案3】:

      您可以使用在some-branch 上显示最近提交的日期

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

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

      【讨论】:

        猜你喜欢
        • 2017-12-02
        • 2010-11-28
        • 2016-05-02
        • 1970-01-01
        • 2013-04-24
        • 2021-03-15
        • 2013-11-28
        • 2011-12-16
        相关资源
        最近更新 更多