14

I'm sure this can be done (?) (in clearcase it would be quite simple).

c-urchin
  • 4,344
  • 6
  • 28
  • 30
  • 3
    Note: In distributed systems (like Git) the recorded dates and times may not be 100% reliable or accurate since they may be recorded on a myriad of machines. Hopefully most machines are reasonably close to the “real time”, but there may be oddball outliers lurking in the network of contributors. – Chris Johnsen Jul 02 '10 at 11:59

3 Answers3

15

Use git log to determine a suitable revision to switch to, e.g.:

git log --since='2010-04-01' --until='2010-04-02'

This will show all the commits on 2010-04-01, so just pick the one that corresponds to the instant you want the files for, and note its commit id. Then just use git checkout COMMIT-ID to switch the workspace to that commit. This will detach your workspace (HEAD) from the current branch, use git checkout master to return.

araqnid
  • 127,052
  • 24
  • 157
  • 134
  • I've currently run into the problem that `git checkout master@{}` does not work (as the selected date is not in the reflog). Your solution would remedy this, But is this the *only* solution to this? It seems a bit clunky to use two commands with a bit of parsing in between. – exhuma Apr 12 '17 at 08:27
  • ... I suppose, something like this works: `git checkout $(git log -n 1 --until='2017-04-02:00:00:00' --pretty='%H')` but still looks rather hacky. – exhuma Apr 12 '17 at 08:35
7

Don't forget file timestamp are not recorded in a DVCS like Git.
Only commit timestamp are there, and you can checkout easily a commit from a certain date.

git checkout master@{1 month 2 weeks 3 days 1 hour 1 second ago}

(Note: such a checkout would give you a detached HEAD)


In ClearCase, this is easy provided you set the "preserve file time" option to true.
(if not, you actually record the checkin time of each file, which is a bit like the Git commit timestamp, except for every files)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Ok, but there's an additional complication. My repository is a clone of a remote repository. When I give a date, it says that the log only goes back to the date when my repository was cloned. How can I make use of the history of the remote repository? – c-urchin Jul 01 '10 at 20:28
  • @steve: by using the remote refspec instead of a local one. See http://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html#_specifying_revisions: `git checkout /refs/remotes/master@{...}` – VonC Jul 01 '10 at 20:49
  • fatal: '/refs/remote/master@{2010-06-04}' is outside repository – c-urchin Jul 01 '10 at 21:00
  • @steve: true, I don't have yet the right syntax. Still looking. – VonC Jul 01 '10 at 21:30
  • The `ref@{time}` syntax uses the reflog. Reflogs are local. That is why steve's log only goes back to the date it was cloned. – Chris Johnsen Jul 02 '10 at 11:51
3

first, you have to get the string that identify the commit:

git rev-list -n 1 --before="2009-07-27 13:37" origin/master

it prints the string (for instance XXXX), copy it and do this command:

git checkout XXXX
Luca C.
  • 11,714
  • 1
  • 86
  • 77