I would like to calculate the time between commits in git, this (I hope) will give me a rough measure of my productivity. Is this possible?
-
If you want to measure productivity, it might be a nice idea to use the commit hooks to automatically update a database or a csv file somewhere that contains time data which you can later graph. – Noufal Ibrahim Mar 29 '14 at 04:19
3 Answers
This will print the date of the newer commit followed by the date of the older commit:
git log HEAD~[newer_commit] -n 1 --format=%ad; \
git log HEAD~[older_commit] -n 1 --format=%ad
You'll end up with date strings. Use whatever language you like to calculate the difference.

- 913
- 4
- 12
Here is a more complete example of using bash
and date
for doing this.
First, as noted in Git: want to iterate through all commits on branch, and list files in each commit :
git rev-list will list all revisions reachable from a given commit in reverse chronological order, so you can pass it a branch name to get the list for that branch head backwards
So, basically, the idea is that I use rev-list
to iterate from HEAD backwards through the repo history, and at each revision, get the timestamp of current, and the timestamp of previous commit; get them as UNIX timestamps because then we can do integer arithmetic (difference) directly in bash
; and finally use date
to display the dates. So this bash
script would be:
for ix in `git rev-list master`; do
thists=`git log $ix -n 1 --format=%ct`;
prevts=`git log $ix~1 -n 1 --format=%ct 2>/dev/null`;
if [ ! -z "$prevts" ] ; then
delta=$(( $thists - $prevts ));
echo `date -d @$thists +'%Y-%m-%d %H:%M:%S'` "-->" \
`date -d @$prevts +'%Y-%m-%d %H:%M:%S'` " ;= " \
`date -d @$delta +'%H:%M:%S'`;
fi;
done
Note that when you're at the first commit, there is no previous/parent commit, and so git log
will fail with: fatal: ambiguous argument '45e95c7...~1': unknown revision or path not in the working tree.
; to avoid printing this message, I redirect the stderr
to /dev/null
in the prevts
command, and then do the time delta calculation only if prevts
is not empty. The output from this script is something like this:
2017-08-27 09:44:04 --> 2017-08-26 17:12:22 ;= 17:31:42
2017-08-26 17:12:22 --> 2017-08-26 16:48:24 ;= 01:23:58
2017-08-26 16:48:24 --> 2017-08-26 16:28:05 ;= 01:20:19
...
However, these deltas are one hour off - so for more correct display of the time delta, I used Python's datetime.timedelta
-- so in the above script, you may want to replace the `date -d @$delta +'%H:%M:%S'`;
line with:
`python -c "import datetime as dt; d = dt.timedelta(seconds=$delta); print(d)"`
... in which case this log is generated:
2017-08-27 09:44:04 --> 2017-08-26 17:12:22 ;= 16:31:42
2017-08-26 17:12:22 --> 2017-08-26 16:48:24 ;= 0:23:58
2017-08-26 16:48:24 --> 2017-08-26 16:28:05 ;= 0:20:19
...

- 36,975
- 46
- 198
- 278
-
For me the deltas aren't one hour off but 19 hours off, meaning a 7-minute difference shows as 19:07:00 and a 9-hour difference shows as 04:00:00. Bitbucket's git shell doesn't have Python, so I just changed the delta line to subtract 19 hours in seconds: `delta=$(( $thists - $prevts - 68400 ));` I also added a command to print *which* commit caused the elapsed time: `git log $ix -n 1 --pretty=medium`. – Noumenon Oct 29 '18 at 17:42
If you can send the output of git log
to a file, then you're halfway there already. I'm not sure what language you're most comfortable with, but I know it's easy to do in a bash script.

- 2,471
- 3
- 23
- 28