git clone changes file modification time

后端 未结 6 888
谎友^
谎友^ 2021-02-02 06:37

When I clone a git repository using \"git clone ...\" command all cloned files in my local repository have the same modification time with date and time when

相关标签:
6条回答
  • 2021-02-02 07:08

    This linux one-liner will fix all the files (not folders - just files) - and it will also fix files with spaces in them too:-

    git ls-files -z | xargs -0 -n1 -I{} -- git log -1 --format="%ai {}" {} | perl -ne 'chomp;next if(/'"'"'/);($d,$f)=(/(^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d(?: \+\d\d\d\d|)) (.*)/);print "d=$d f=$f\n"; `touch -d "$d" '"'"'$f'"'"'`;' 
    
    0 讨论(0)
  • 2021-02-02 07:14

    Git does not record timestamp for the files, since it is a Distributed VCS (meaning the time on your computer can be different from mine: there is no "central" notion of time and date)

    The official argument for not recording that metadata is explained in this answer.

    But you can find scripts which will attempt to restore a meaningful date, like this one (or a simpler version of the same idea).

    0 讨论(0)
  • 2021-02-02 07:14

    You can retrieve the last modification date of all files in a git repository. (lat commit time) https://serverfault.com/q/401437/267639

    Then use touch command change the modification date.

    git ls-tree -r --name-only HEAD | while read filename; do 
      unixtime=$(git log -1 --format="%at" -- "${filename}")
      touchtime=$(date -d @$unixtime +'%Y%m%d%H%M.%S')
      touch -t ${touchtime} "${filename}"
    done
    

    Also my gist here.

    Oct 2019 Update

    Thanks to P. T. for your comment.
    I've updated the answer and gist to support filenames with space.

    0 讨论(0)
  • 2021-02-02 07:15

    Adding to the list of one-liners ...

    for f in $(git ls-files) ; do touch -d $(git log -1 --format='%aI' "$f") "$f" ; done
    
    0 讨论(0)
  • 2021-02-02 07:25

    Another option for resetting the mtime is git-restore-mtime.

    sudo apt install git-restore-mtime # Debian/Ubuntu example
    git clone <myurl>
    cd <mydir>
    git restore-mtime
    
    0 讨论(0)
  • 2021-02-02 07:25

    A shorter variant of @Chris's answer that I find easier to understand:

    git ls-files | xargs -I{} git log -1 --date=format:%Y%m%d%H%M.%S --format='touch -t %ad "{}"' "{}" | $SHELL
    
    0 讨论(0)
提交回复
热议问题