How to get git to show commits in a specified date range for author date?
GitGit ShowGit Problem Overview
Apparently this:
git log --all --after="<date> 00:00" --before="<date> 23:59" --author="<author>"
filters commits based on the committer date. How can I make it show commits for a specified author date range ?
Git Solutions
Solution 1 - Git
Maybe I'm missing something, but wouldn't this be enough?
git log --pretty=format:"%ad - %an: %s" --after="2016-01-31" --until="2017-03-10" --author="John Doe"
See also here
Solution 2 - Git
You can't—at least, not in Git alone. (Reminder to others visiting this question: it's not about viewing the author date, it's about selecting commits by the author date, a la --since
/--after
and --until
/--before
. These selectors use the committer date, not the author date. Consider as an extreme example a commit made "now", so that its committer date is in the 2000s, but backdated in the author-date field to some day in the year 1999. If your selection range is "any time near the turn of the century" you'll de-select this commit, since its committer date is "now", more than a decade beyond 1999.)
I consider this a small bug in Git: you should be able to request that it use the author date field anywhere you can request that it use the committer date field. This is easy with log formatting, since we have %ad
and %cd
and the like, but impossible with commit selection. The closest we have is that git rev-list
can sort by author-date (within general topo-sorting).
A global switch in git rev-list
, like --use-author-date
, would work as a simple patch, and would not be too hard to add to Git, but I think it would be better to have --min-author-age
and --max-author-age
or similar, and a "sort by author date" flag (independent of the general --topo-order
flag, so that setting both flags has the same effect as --author-date-order
).
As a workaround, you can list all potentially-interesting commits (with git rev-list
or equivalent, such as git log
: use whatever specifier makes commits potentially interesting, except for date filters: in this case that's just --all
) and extract all of their author-date fields (with git log --format=%at
or whatever), then do your own pruning of the list of commit IDs, then re-submit the remaining commit IDs to git log --no-walk
. But this is painful at best. See Tim Beigeleisen's answer using awk for more.
Solution 3 - Git
You can.
But as @torek mentioned, you might not be able to do this with pure Git. One option would be to pipe some pretty format output from git log
into awk
, and check the author date there:
git log --date=iso --pretty=format:'%ad%x08%aN' | awk '$0 >= "2013-01-01" && $0 <= "2013-12-01"'
Here, the %ad
gives the author date in ISO format, and %aN
gives the author name.
Solution 4 - Git
inspired by Tim Biegeleisen's answer above.
git log --all --date=iso --pretty=format:'%ad%x08%aN %s' | grep 2020-06-09 | sort -u
did what I needed. I want author's date, not commits date because I rebase and squash a lot. but I keep important dates on import commits.
related to that, I have a small helper allowing me to reset the author's email but keeping the original author's date. --reset-author by default overwrites the author's date.
https://github.com/mathieujobin/git-scripts/blob/master/bin/git-reset-author-but-not-date
on the git log above, when needed, you can add %H
to get the gitsha
Solution 5 - Git
For those looking for a quick copy-and-paste solution:
git log --pretty='%aI %H' | \
awk '$1 >= "<after-date>" && $1 <= "<before-date>" { print $2 }' | \
git log --no-walk --stdin
Notes:
- The
<after-date>
or<before-date>
portions can be left off as needed - Dates must be in strict ISO format (
YYYY-MM-DDThh:mm:ss
e.g.2021-04-20T13:30:00
) - Dates may be truncated, but similarly to
--after
/--before
, they will always round down to an exact time. Therefore, to find all commits on a particular day, for example, one of the following is required:$1 >= "2021-04-20T00:00:00" && $1 <= "2021-04-20T23:59:59"
$1 >= "2021-04-20" && $1 < "2021-04-21"
(notice<
instead of<=
)
- The date comparison does not take time zone into account. If the repo has commits from different time zones and you need the precision, use your local time zone with
git log --date=iso-strict-local --pretty='%ad %H'
, or you can specify dates by unix timestamp and use%at
.
Based on this answer by torek, which was also contributed to by Mr_and_Mrs_D and Tim Biegeleisen (all involved on this question).