Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see the date multiple files were created on git?

Tags:

git

github

I want to see the date of git creation (date of first commit where they were added) of all the files on a specified directory.

like image 762
polvoazul Avatar asked Jun 11 '12 06:06

polvoazul


People also ask

What is the command to check the history of a git repository?

`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.

Does git track file timestamps?

And Git doesn't preserve timestamps—meaning, when I checkout a branch with an earlier version of a file, that file's timestamp now matches the time now (when I checked out the branch) and not the time of the last commit in which the file changed.

Where does git keep history?

Git stores the complete history of your files for a project in a special directory (a.k.a. a folder) called a repository, or repo. This repo is usually in a hidden folder called . git sitting next to your files.


1 Answers

I'll break my solution into steps.

Get a list of all files in the repository

$ git ls-files

This returns a list of relative paths of all files in the repository.

Get the SHA-1 of the first commit of a given file:

$ git rev-list HEAD <file> | tail -n 1

This will return a list of all parentless commits for a given file, in reverse chronological order. The last one is the SHA-1 hash of the first commit for the given file.

You can verify this by running git log --raw <hash>. You should see something like:

commit <commit_hash>
Author: Susy Q <[email protected]>
Date:   Wed Aug 24 12:36:34 2011 -0400

    Add new module 'example.py'

:000000 100644 0000000... <hash>... A  example.py

Show the date of a given commit

$ git show -s --format="%ci" <hash>

Bringing it all together in a bash script:

#!/bin/bash
for file in $(git ls-files)
do
    HASH=$(git rev-list HEAD "$file" | tail -n 1)
    DATE=$(git show -s --format="%ci" $HASH --)
    printf "%-35s %s\n  %s\n" "$file" $HASH: "$DATE"
done
like image 110
David Cain Avatar answered Sep 23 '22 08:09

David Cain