git clone 更改文件修改时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21735435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git clone changes file modification time
提问by user3302761
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 git clone
command was issued.
当我使用“ git clone ...
”命令克隆git 存储库时,本地存储库中的所有克隆文件的修改时间与git clone
发出命令的日期和时间相同。
Is there a way to clone remote git repository with actual modification time for each file?
有没有办法使用每个文件的实际修改时间来克隆远程 git 存储库?
回答by VonC
Git does not record timestamp for the files, since it is a DistributedVCS (meaning the time on your computer can be different from mine: there is no "central" notion of time and date)
Git不记录文件的时间戳,因为它是分布式VCS(意味着您计算机上的时间可能与我的不同:没有时间和日期的“中心”概念)
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).
回答by ERU
You can retrieve the last modification date of all files in a git repository. (lat commit time) https://serverfault.com/q/401437/267639
您可以检索 git 存储库中所有文件的最后修改日期。(lat 提交时间) https://serverfault.com/q/401437/267639
Then use touch
command change the modification date.
然后使用touch
命令更改修改日期。
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
2019 年 10 月更新
Thanks to P. T.for your comment.
I've updated the answer and gist to support filenames with space.
感谢PT的评论。
我已经更新了答案和要点以支持带空格的文件名。
回答by Chris
This linux one-liner will fix all the files (not folders - just files) - and it will also fix files with spaces in them too:-
这个 linux one-liner 将修复所有文件(不是文件夹 - 只是文件)-它也将修复其中包含空格的文件:-
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'"'"'`;'