2

I know that e.g. with

git shortlog -sn --since=3.months <branch>

I can get the commits done in the last 3 months on <branch> counted from now. But how can I make the 3.months count from the date of the last commit on <branch> instead? Is there a syntax for this to work in a single command, or would it require multiple git invocations?

sschuberth
  • 28,386
  • 6
  • 101
  • 146

1 Answers1

2

Get the date of the last commit on the branch with

git log --format='%ai' <branch> -n1
  • --format='%ai' will show only the author date. Author date is the date the commit was originally made. It is not affected by things like rebasing. If you want the commit date, use %ci.
  • -n1 says to only show the latest commit in the branch.

date can do date math, so use that to subtract 3 months. Then feed that to --since.

git log --since "$(date --date "$(git log --format='%ai' <branch> -n1) - 3 months")" <branch>

You will probably want to turn this into an alias.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • I tried `git log --format='%ai' main -n1` didn't produce anything. what did I do wrong? – Sentry.co Jun 30 '23 at 13:29
  • @Sentry.co Dunno. You should get something, or `fatal: ambiguous argument 'main': unknown revision or path not in the working tree` if main does not exist. Does `git log --format=fuller -n1` give anything, and does that commit have an Author Date? – Schwern Jun 30 '23 at 13:54
  • 1
    @Sentry.co It's also possible something about your pager or terminal is interfering and eating the line. Try `-n5` to show 5 lines and see what happens. – Schwern Jun 30 '23 at 13:58
  • Thanks. I found another one that worked https://stackoverflow.com/a/13542327/5389500 I suppose I can add the date command to that. – Sentry.co Jun 30 '23 at 14:53