0

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
vel
  • 1,000
  • 1
  • 13
  • 35
  • 1
    One remark : unless you intend to use it in a function or a script, there is little interest in feeding arguments using `$( ... )` from the command line. Since you don't see what you are passing, it mostly makes your command less easy to debug. If I understand correctly : you want to run `git checkout -b mynewbranch2 84e85` (where `84e85` is the ID of the commit at 16:13:47) – LeGEC Jun 02 '22 at 09:57
  • To debug your command : have you checked the output of `$ git rev-list -n 1 --first-parent --before="2022-05-09 16:13:47" mynewbranch1` ? or `git log -n 1 --first-parent --before="2022-05-09 16:13:47" mynewbranch1` ? – LeGEC Jun 02 '22 at 10:03
  • @LeGEC thanks for your involvement. Yes that is correct you are targeting the right commit id in your first comment. Second thing: for both functions I am getting empty response. I think what might be specific here but please help me understand what should be my next steps. We are actually migrating git commits from some other system - so every my `git commit` contains `--date` property where for example on 11st May I am migrating some commit from 1st may -. e.g . `git commit -m "message" --date="2022-05-01 11:11:11" and that commit I then cannot see through the `rev-list` – vel Jun 02 '22 at 10:12
  • I am not sure if anyone had this kind of issue - basically I think I figured out that rev list and git log filtering are only displaying me the results from the 11st and 12th May when I run the script to migrate git commits from some XML file which contains older timestamps from those date - so I set in `git commit` command `--date` property with older timestampt and in standard log of that branch I can see the output like from the examples in the Question, but `rev-list` is ignoring those transactions! – vel Jun 02 '22 at 10:15
  • Are you migrating from git to git ? – LeGEC Jun 02 '22 at 10:16
  • No, from accurev (old SCM system) to git. AccuRev just provides me a XML with all the transactions (commits) with Author Name and Author Date! – vel Jun 02 '22 at 10:20

1 Answers1

2

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 or git 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 %ad and %cd :

git log --graph --format="%h author=%an %ad (committer=%cn %cd) %s"

You specified in your comment that you are re-creating commits from an external source.

At a commit's creation, you use environment variables to also override the committer :

GIT_AUTHOR_NAME=alex \
GIT_AUTHOR_EMAIL=alex@somewhere \
GIT_AUTHOR_DATE="2022-05-09 13:47:00" \
GIT_COMMITTER_NAME=alex \
GIT_COMMITTER_EMAIL=alex@somewhere \
GIT_COMMITTER_DATE="2022-05-09 13:47:00" \
git commit -m "..."

https://git-scm.com/docs/git-commit#_commit_information

If you have already migrated part of your history, you can rewrite existing commits, using git rebase -x ... or git-filter-repo (link to docs: check the CALLBACKS section) for example.


additionally : I highly suggest you add something (it seems your previous system had some "transaction id" ?) in the git commit message to allow for a more precise match of migrated commits.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • thanks for your involvement. Yes that is correct you are targeting the right commit id in your first comment. Second thing: for both functions I am getting empty response. I think what might be specific here but please help me understand what should be my next steps. We are actually migrating git commits from some other system - so every my `git commit` contains `--date` property where for example on 11st May I am migrating some commit from 1st may -. e.g . `git commit -m "message" --date="2022-05-01 11:11:11" and that commit I then cannot see through the `rev-list` – vel Jun 02 '22 at 10:16
  • LeGEC I think you are completely right! Thanks a lot!!! The only thing now is how I can filter commits using that other date (which was explicitly set with `--date` property when creating a commit? Is that possible at all??? Can I filter based on `Author Date`? Maybe it's not I do not know... – vel Jun 02 '22 at 10:18
  • @AndreyS: if you want to recreate new commits from a list of (something ?) I would advise you to put something in the commit message that identifies its source (thingy ?), (e.g: "\n\n migrated from #xyz") and look for *that* in git log. – LeGEC Jun 02 '22 at 10:21
  • You are definetely right!!! Example: `AuthorDate: Mon May 9 16:13:47 2022 +0200 CommitDate: Wed May 11 13:48:22 2022 +0200` That is why this is not included for my desired commit hash. I will also put in the Question what is the main cause of the issue – vel Jun 02 '22 at 10:21
  • you are completely right - I even put in the message transaction (commit ID) from the previous system - and I filtered that in that way! But now it's still required that I must filter explicitly by the date itself, not by some part of the message... unfortunately :( – vel Jun 02 '22 at 10:23
  • I have updated question with your conclusion. The only thing now is is it possible to filter based on author date or not... – vel Jun 02 '22 at 10:38
  • 1
    @AndreyS : I also updated my answer : you can set the committer date too (use something that recreates commits to "update" commits you already created). If you have more specific questions about your migration, please describe your needs and issues in a new question. – LeGEC Jun 02 '22 at 10:56