35

Im trying to find the command to get the last modified date of a file in a local git repo.

Created the repo and have done one commit. Had to edit one file and will commit this file but was wondering what the last modification data of the file in the repo is (not the commit date).

Have no gui onl command line.

git log ./path/to/filename.php

only gives me the commit date

HMR
  • 34,271
  • 21
  • 76
  • 150
  • Git works in terms of blobs, not files. Each commit corresponds to a blob and can contain many files, all of which are created atomically. Git log should give you the full log of the file, the earliest date is the file creation date in Git. – hd1 Mar 19 '14 at 06:09
  • Would "ls -l" not do the trick? – Paul Hicks Mar 19 '14 at 06:09
  • @PaulHicks No, I just modified the file. I would like to know last modification date of the file in the git repo. Doesn't git maintain versions of files and their properties? (like last mod date?). Maybe it doesn't though. – HMR Mar 19 '14 at 06:27
  • @hd1 I guess the file properties aren't saved so I only know the last commit date. Wanted to know when the last mod date of the file in git was but I guess I should have checked that before editing the file :-) – HMR Mar 19 '14 at 06:30
  • @HMR I just pointed out (in [my answer below](http://stackoverflow.com/a/22498088/6309)) a script which *can* save those timestamps, and restore them on checkout; – VonC Mar 19 '14 at 06:36

4 Answers4

66

The correct way to do this is to use git log as follows.

git log -1 --pretty="format:%ci" /path/to/repo/anyfile.any

-1 restricts it to the very last time the file changed

%ci is just one of the date formats you can choose from others here at https://git-scm.com/docs/pretty-formats

This method is fool proof and 100% accurate.

MitchellK
  • 1,952
  • 1
  • 15
  • 25
  • What if I would like to list last commit date for each file in my current directory? Like `ls -la`, except using `git` modification date rather than system modification date. – Apollys supports Monica Oct 28 '19 at 23:30
  • This is the answer I was looking for. I just wouldn't say it is fool proof and 100% accurate, what if an older commit is rebased onto a newer commit? It will show the timestamp of the last and thus in this case older commit. This is what I would expect, because git is a proper time machine, but users should be aware of that. – Christian Hujer Nov 21 '19 at 09:56
  • 2
    Your current working directory needs to be in the git repo, otherwise you get `fatal: not a git repository (or any of the parent directories): .git` – Boris Verkhovskiy Feb 03 '20 at 20:30
  • 2
    @Boris Instead of changing the current directory, you can also specify `-C /some/git/dir` to let git know where the repo is located. – Stéphane May 25 '20 at 20:15
  • 1
    I partly agree with your statement regarding the fool proof; it is 100 % good if you remove the `)`. – Timo Mar 24 '22 at 20:40
6

MitchellK's answer exactly fit my needs, setting my local files' last written times to what's in git. Here's a little C# LinqPad script to automate the process:

var root = new DirectoryInfo(@"C:\gitlab\mydirectory\");

Directory.SetCurrentDirectory(root.FullName); // Give git some context

var files = root.GetFiles("*.*", SearchOption.AllDirectories);

foreach (var file in files)
{
  var results = Util.Cmd("git", 
                         $"log -1 --pretty=\"format:%ci\" \"{file.FullName}\"",
                         true);
  
  var lastUpdatedString = results.FirstOrDefault();
  if (lastUpdatedString == null)
  {
    Console.WriteLine($"{file.FullName} did not have a last updated date!!");
    continue;
  }
  
  var dt = DateTimeOffset.Parse(lastUpdatedString);
  if (file.LastWriteTimeUtc != dt.UtcDateTime)
  {
    Console.WriteLine($"{file.FullName}, {file.LastWriteTimeUtc} => {dt.UtcDateTime}");
    file.LastWriteTimeUtc = dt.UtcDateTime;
  }
  else
  {
    Console.WriteLine($"{file.FullName} already updated.");
  }
}
Marcus Mangelsdorf
  • 2,599
  • 1
  • 29
  • 39
David Peters
  • 1,817
  • 1
  • 19
  • 18
4

Get the last modified timestamp

git log -1 --pretty="format:%ct" /path/to/repo/anyfile.any)
Jože Ws
  • 1,627
  • 14
  • 12
0

Git doesn't record the last modification date, only the commit/author dates for a all commit (which can include more than one file).

You would need to run a script in order to amend a commit with the last modification date of a particular file (not very useful if said commit has more than one file in it).
See an example at "Git: Change timestamp after pushing".

Another option would be to record those timestamp in a separate file, and amend your commit that way: see "What's the equivalent of use-commit-times for git?".

That includes:

  1. mtimestore - core script providing 3 options:
    • -a (save all - for initialization in already existing repo (works with git-versed files)),
    • -s (to save staged changes), and
    • -r to restore them.
  2. pre-commit hook
  3. post-checkout hook

Incidentally, this is the post where I explained 5 years ago why Git doesn't record timestamps.

Community
  • 1
  • 1
VonC
  • 1,129,465
  • 480
  • 4,036
  • 4,755
  • Thank you for your answer. I will from here on out check the last change date first. If I do `git add .` and then `git status` I'll get all the files changed since last commit anyway. – HMR Mar 19 '14 at 13:22