sds
sds

Reputation: 60004

How do I save the original creation time in git?

I have about 30 older *.ipynb files which I want to start tracking in git.

I want to save as much metadata as possible, e.g., what is the best way to record their original timestamp in git?

The way this timestamp (in fact, just the mtime of the file) would be used is figuring out which experimental approaches were tried in what order (and, thus, which are more likely to be useful). Thus, I might want to order the files by their original timestamp.

PS. In this context the file mtime becomes, in fact, git ctime. :-)

Upvotes: 2

Views: 233

Answers (2)

sds
sds

Reputation: 60004

Method

I can store the timestamp as the AuthorDate of the first commit. Since the files are *.ipynb notebooks, they are not going to be mentioned in any Makefile, and I want to do it just once, on the initial checkin, so Linus's vehement objection quoted in What's the equivalent of use-commit-times for git? is not applicable in any form.

Store

1 - get the list of files to operate on:

LEGACY_IPYNB=$(git ls-files --others --exclude-standard | grep ipynb)

2 - add the files with git add $LEGACY_IPYNB.

3 - commit each file separately (YUK! but I cannot do a single commit because the author time is per commit, not per file!):

for f in $LEGACY_IPYNB; do
  git commit --date=$(stat -f "%Sm" -t "%FT%T" $f) \
      --author="the actual author" -m 'initial commit' $f
done

Recover

1 - extract the initial commit ID (How to reference the initial commit?):

# FIXME: will break if `$f` has been since renamed!
COMMIT=$(git rev-list --max-parents=0 HEAD -- $f)

2 - extract the initial author time:

MTIME=$(git log --pretty=format:%ad --date=format:%Y%m%d%H%M $COMMIT)

3 - modify mtime of the checked out file:

touch -m -t $MTIME $f

Upvotes: 1

Patryk Obara
Patryk Obara

Reputation: 1847

By default git stores only file mode, but you can attach textual data to any object you like - this feature is called "notes". You can attach note to any object (including blob), but probably best way is to attach it to commit introducing your file, as you will be able to easily see note content with git log.

$ git notes add <hash>

See manual page to read about all the options :)

Upvotes: 2

Related Questions