130

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 ?

Community
  • 1
  • 1
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
  • Possible duplicate of [Git log: filter by commit's author date](https://stackoverflow.com/questions/13987273/git-log-filter-by-commits-author-date) – underscore_d Feb 11 '18 at 15:11

5 Answers5

176

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

jackdbd
  • 4,583
  • 3
  • 26
  • 36
  • 28
    @RossSolomon: that *shows* the author date, but *selects by* the committer date. To take an extreme example, suppose I make a commit with author date set to 1999 and committer date set to 01-01-2017. The `%ad` directive will show you this authored-in-1999 commit, even though that is way before 2016! – torek Oct 08 '17 at 21:37
  • That command doesn't work with the iso-8601 date format in the --before and --after. The format that works for me is "dd-mm-ccyy". – hktegner May 04 '18 at 11:43
  • 11
    Downvoting because this answer does not address the question, despite being useful in general. – ravron Jun 25 '18 at 19:13
  • 3
    Even if it does not answer the question, if I happened to stumble upon this answer and found a solution to my *similar-but-not-the-same* problem, I'm upvoting the answer, simple as that. However, this does not mean that the answer should be accepted, and I understand someone downvoting it. – Mefitico Jan 18 '19 at 11:39
  • I like to add --reverse to sort them from oldest to newest – sicko Oct 02 '20 at 10:35
63

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.

torek
  • 448,244
  • 59
  • 642
  • 775
  • 1
    Thanks for your authority clearing up the confusion for me - this has been bugging me for long and I did spend a lot of time going through the man pages to try and find the non existing switch. Is there a bug already open for this ? – Mr_and_Mrs_D May 19 '16 at 08:34
  • I don't know if the Git folks consider this a bug. See https://github.com/git/git for links to the mailing lists. – torek May 19 '16 at 17:33
  • Thank you for this answer. I also tried different ways and debug but not worked for me. – Deep Dalsania Jun 13 '21 at 10:55
  • It is so sad that there is not such useful option to list by author date for a long time =( – Eugen Konkov Apr 07 '23 at 01:59
45

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 2
    Thanks - I went ahead and accepted Torek's answer as more comprehensive but upvoted yours for giving a starting point :) – Mr_and_Mrs_D May 19 '16 at 08:34
  • The problem with this approach is that it only works with one-line outputs. With `log -p`, for example, it won't do. – Sergei Tachenov Jul 10 '21 at 05:19
  • @SergeiTachenov Thanks for the comment, and I would suggest not moving to Houston. NASA and the US are way past their peak, and the glory days of the Apollo era are probably not coming back. – Tim Biegeleisen Jul 10 '21 at 05:30
  • @TimBiegeleisen, ha-ha! It's not like I have any chances of really moving there! And compared to Russian space industry I'm working at, pretty much _anything_ is state-of-the art, really. – Sergei Tachenov Jul 11 '21 at 07:02
  • Honestly, maybe have a look at the Chinese Space agency. They just launched a space station, plus China is only a short plane ride from Russia. – Tim Biegeleisen Jul 11 '21 at 07:55
7

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).

Josh Klodnicki
  • 585
  • 6
  • 18
5

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

Mathieu J.
  • 1,932
  • 19
  • 29
  • 3
    %x08 in pretty format means ascii 08 which is backspace, so it eats the last digit of previous %ad which is author date in ISO format, so I use this variation (also added colon after author name) : `git log --all --date=iso --pretty=format:'%ad - %aN : %s' | grep 2020-09 | sort -u` – sicko Oct 02 '20 at 11:07