231

I've already looked at the relevant docs from git-scm.com and gitref.org, but I can't seem to figure this out.

Let's say I want to get all commits for Tuesday, November 12th, 2013. Using an existing repo as an example, I know for a fact that I have commits on that day, as well as commits the day before and the day after.

With 2013-11-11 and 2013-11-12

All the following give me commits for both November 11th and 12th:

  • git log --after="2013-11-11" --until="2013-11-12"
  • git log --since="2013-11-11" --until="2013-11-12"
  • git log --after="2013-11-11" --before="2013-11-12"
  • git log --since="2013-11-11" --before="2013-11-12"

With 2013-11-12 only

All the following give me no commits:

  • git log --since="2013-11-12" --until="2013-11-12"
  • git log --since="2013-11-12" --before="2013-11-12"
  • git log --after="2013-11-12" --until="2013-11-12"
  • git log --after="2013-11-12" --before="2013-11-12"

With 2013-11-12 and 2013-11-13

As expected (from the results of 2013-11-11 and 2013-11-12 above), all of the following give me results from both November 12th and 13th:

  • git log --since="2013-11-12" --before="2013-11-13"
  • git log --after="2013-11-12" --before="2013-11-13"
  • git log --since="2013-11-12" --until="2013-11-13"
  • git log --after="2013-11-12" --before="2013-11-13"

...and so on and so forth. I feel like I've tried every possible combination of since, after, before, and until but still can't find the answer, nor do I understand whether those options are inclusive or exclusive, since they seem to be inclusive if the two dates are different, but exclusive if they're on the same day. Did I miss something / what am I doing wrong?!

4

5 回答 5

302

谢谢约翰·巴塞洛缪!

答案是指定时间,例如git log --after="2013-11-12 00:00" --before="2013-11-12 23:59"

于 2013-11-14T20:07:32.410 回答
43

我通常会检查我的 git 日志并查看我在特定日期的工作,并据此更新我的时间表,但是以 ISO 格式输入完整日期是一件很痛苦的事情,所以我只是这样做

git log --after=jun9 --before=jun10

我添加--author只打印我的提交

git log --since=jun9 --until=jun10 --author=Robert 

这将打印 6 月 9 日最后发生的提交(在这种情况下是 2016 年,而不是 2015 年或 2014 年等等)。

--since/--afterand--until/--before参数也可以采用,3 days agoyesterday

于 2016-06-14T07:31:03.897 回答
20

接受的答案没有错(我赞成),但是......我们是为了科学而来的!

下面的输出可以使用pretty=format:<string> 占位符进行扩展/自定义:

git log --pretty='format:%H %an %ae %ai' | grep 2013-11-12

由于用户可能输入了相同的字符串,因此无法 100% 避免错误。但可以接受,具体取决于使用的占位符。例如,上面的代码段不会失败。

一个人也可以只解析整个数据并将其数据消费/操作git logJSON一个人的内心内容。检查https://github.com/dreamyguy/gitlogg并且永不回头!

免责声明:这是我的项目之一。

于 2016-05-21T23:35:49.360 回答
17

我为此特定目的制作了一个git 别名:

commitsAtDate = "!f() { git log --pretty='format:%C(yellow)%h %G? %ad%Cred%d %Creset%s%C(cyan) [%cn]' --decorate --after=\"$1 0:00\" --before=\"$1 23:59\" --author \"`git config user.name`\"; }; f"

用法:

git commitsAtDate 2017-08-18
于 2017-08-18T12:57:09.527 回答
-2

此脚本显示当前 repo 的可用提交日期范围,然后提示您输入要查看提交的日期。它显示一个简短的 SHA 和完整的 SHA、作者、提交时间戳和单引号中的注释。

该脚本会一直提示输入日期,直到您按 Enter 或 Control-D。

Mac 用户:需要 gnu 日期。

#!/bin/bash

COMMITS=`git log --abbrev-commit --pretty="format:%h %H %ai" | sort -k3 -k4`
FIRST=`echo "$COMMITS" | head -n 1`
LAST=`echo "$COMMITS" | tail -n 1`
echo "First commit: $FIRST"
echo "Last commit: $LAST"
printf "Date to search for commits: "
read DATE
while [[ "$DATE" ]]; do
  NEXT_DATE=`date +%Y-%m-%d -d "$DATE +1 day"`
  #echo "Searching for commits from $DATE to $NEXT_DATE"
  echo `git log --after="$DATE" --before="$NEXT_DATE" --pretty="format:%h %H %an %ci '%s'"`
  printf "\nDate to search for commits: "
  read DATE
done

我调用了脚本commitsOnDates,它正在运行。我输入的第一个日期没有提交,所以响应只是一个空行:

$ commitsOnDates
First commit: 375bcfb 375bcfbbf548134a4e34c36e3f28d87c53b2445f 2015-08-03 13:37:16 -0700
Last commit: 1d4c88c 1d4c88ce6a15efaceda1d653eed3346fcae8f5e6 2018-10-13 21:32:27 -0700
Date to search for commits: 2015-08-13


Date to search for commits: 2015-08-03
375bcfb 375bcfbbf548134a4e34c36e3f28d87c53b2445f Mike Slinn 2015-08-03 13:37:16 -0700 'this is a comment'

Date to search for commits: 2018-10-13
1d4c88c 1d4c88ce6a15efaceda1d653eed3346fcae8f5e6 Mike Slinn 2018-10-13 21:32:27 -0700 'a comment' 64d6e16 64d6e16338657b82c91ac94bea8ebf7b80dc4c28 Mike Slinn 2018-10-13 18:28:41 -0700 'nother comment' d5eb26e d5eb26e49fc9dceee9b9f5a2d8fa052bff2cfbbc Mike Slinn 2018-10-13 18:16:20 -0700 'no comment' d8a4992 d8a4992df208ba5efb50728311820bdad5ba5332 Mike Slinn 2018-10-13 12:02:00 -0700 'commented'

Date to search for commits:
于 2018-10-14T15:24:02.997 回答