I am using git, trying to sync to the first commits of each month for the last year. I want to be able to go back month by month, on the first day of the month, ON THE FIRST SECOND OF THE MONTH. or at least from some constant "seconds" value. So far I have this:
$(git rev-list --before "$(date -d "$(date +%Y-%m-01) -$i months" +%Y-%m)-01" -n 01 HEAD)
This obviously does not include a constant seconds value. As it stands, running this script and then running it again an hour later returns two different sha1's because it is going back x months FROM THE EXACT TIME at which I run the script. I want the returned sha1's to be the same no matter when I run this script. Does that make sense? Any ideas?
First of all, you have a typo in your command. It has to be --before="$(...
, you are missing the =
.
The behaviour of date
is not the problem here. date +%Y-%m-01
will return something like 2011-10-01
as a string, there's no additional time information included. So the second date call will decrease it by the $i
number of months and will also return a sting of the format like 2011-09-01
. Besides that string no additional information is passed to git-rev-list
as value of argument --before=
.
There are some things you have to consider using git-rev-list
:
HEAD
you're always referring to the current branch. So when you for example checkout one of the obtained commit-IDs your HEAD will change. You maybe want to use master
or any other branch name as reference instead.--before
or --after
arguments since they rely on the timestamp of the committer field.Considering all the said, the following command works for me:
$(git rev-list --after="$(date -d "$(date +%Y-%m-01) -$i months" +%Y-%m)-01 00:00:00" master | tail -n 1)
This will return the ID of the first commit after the first of the given month. (This commit was not necessarily made during that month, maybe there were no commits in that month anyway.)