I have read very well-known link How to checkout in Git by date?
the command git rev-list
doesn't work for me - I am always getting the whole history of the commits, specified date time value seems to be ignored for my case.
Steps:
I am on my branch mynewbranch1
git log
presents these commits:
**commit 1250c4c2 (HEAD -> mynewbranch1)**
Author: john <>
Date: Tue May 10 11:27:45 2022 0200
text1`
**commit ae99ff**
Author: john
Date: Mon May 9 17:22:37 2022 0200
text2`
**commit 84e85**
Author: alex <>
Date: Mon May 9 16:13:47 2022 0200
text3`
**commit 41b35**
Author: alex <>
Date: Mon May 8 16:11:31 2022 0200
text4`
Now I execute following command:
git checkout $(git rev-list -n 1 --first-parent --before="2022-05-09 16:13:47" mynewbranch1)
I tried also with backticks:
git checkout `git rev-list -n 1 --first-parent --before="2022-05-09 16:13:47" mynewbranch1`
Now I want to create a new branch from it:
git checkout -b some_branch_from_above_date
But when I execute git log
on my new branch some_branch_from_above_date
I can again see all the commits! I would expect to have only the 3rd and 4th commit from the above list because only those are before the specified date: 2022-05-09 16:13:47
I must do this through the script so I need to checkout by date! I even tried, for testing purposes, with string syntax -900 days ago but I still get recent commits! Please help!
git checkout -b mynewbranch2 `git rev-list -n 1 --before="900 days ago" mynewbranch1`
UPDATE THANKS TO @LeGEC
Cause for this problem is because rev-list
filters only based on CommitDate. While my AuthorDate is different because I set dat in git commit
command explicitely with --date
option. This is seen with git log --format=fuller
command.
AuthorDate: Thu May 9 16:13:47 2022 0200
CommitDate: Wed May 11 13:48:07 2022 0200
CodePudding user response:
There is a known trap with commit dates :
a commit actually has two dates : the author date, and the committer date.
- when an entirely new commit is created, it gets its author date and committer date set to
now()
, - for commands such as
git commit --amend
,git cherry-pick
orgit rebase
, git creates new commits with a new committer/committer date, but preserves the author/author date from the "copied" commit.
And unfortunately : the date displayed by default by git log
is the author date, but the date used in --before/--after
filter is the committer date ...
AFAIK, there are currently only ways outside of git to filter by author date : see for example these two answers :
A quick way to see both dates explicitly is to use git log --format=fuller
.
Or write a custom format string, using