【问题标题】:git status - list last modified dategit status - 列出最后修改日期
【发布时间】:2013-01-03 14:58:41
【问题描述】:

使用 git,是否可以在路径旁边列出未暂存文件的最后修改日期?使用例如。

git status

git diff --name-only

【问题讨论】:

    标签: git


    【解决方案1】:

    不直接,但你可以使用管道:

    注意:原始答案基于 cmets 更新

    Linux:

    git status -s | while read mode file; do echo $mode $file $(stat -c %y $file); done
    

    窗户:

    git status -s | while read mode file; do echo $mode $(date --reference=$file +"%Y-%m-%d %H:%M:%S") $file; done
    

    OSX (source):

    git status -s | while read mode file; do echo $mode $(stat -f "%Sm" $file) $file; done|sort
    

    【讨论】:

    • 这行得通 - 有一个警告 - 我在 Windows 上使用 msysgit,似乎 stat 不可用。但我发现我可以像这样使用dategit status -s | while read mode file; do echo $mode $(date --reference=$file +"%Y-%m-%d %H:%M:%S") $file; done
    • 在我的 Mac 上,stat -c 似乎不起作用。我是这样做的:git status -s | while read mode file; do echo $mode $(stat -f "%m" $file) $file; done|sort。来源:stackoverflow.com/a/10198387/2486953
    • 不适用于路径和 $file 中的空格。也不起作用:“$file”。
    • 在 macOS catalina 上,此列表文件以及文件大小等,但没有日期。
    • 如果删除了文件,就会出现很多“没有这样的文件或目录”的错误。要忽略已删除的文件,请在 Linux 上使用 git status -s | while read mode file; do if [ "$mode" != "D" ]; then echo $mode $file $(stat -c %y $file); fi; done
    【解决方案2】:

    注意:我需要将修改后的文件按日期排序,所以我修改了回显:

    git status -s | while read mode file; \
      do echo $mode $(stat -c %y $file) $file; \
    done|sort -k1,4
    

    一行:

     git status -s | while read mode file; do echo $mode $(stat -c %y $file) $file; done|sort -k1,4
    

    通过首先回显日期 (stat),然后是文件,我能够从最旧到最新的修改进行排序。


    Sam Hasler 添加in the comments

    在模式下保留空格

    IFS=''; git status -s | while read -n2 mode; read -n1; read file; do echo $mode $(stat -c %y "$file") $file; done|sort
    

    即:

    IFS=''; git status -s | while read -n2 mode; read -n1; read file; \ 
      do echo $mode $(stat -c %y "$file") $file; \ 
    done|sort
    

    【讨论】:

    • mode中保留空格:IFS=''; git status -s | while read -n2 mode; read -n1; read file; do echo $mode $(stat -c %y "$file") $file; done|sort -k1.4
    • @SamHasler 好点。我已将您的评论包含在答案中以提高知名度。
    • 我更喜欢您提出的解决方案。但是,我认为您的排序命令有错误。您应该使用逗号 (sort -k1,4),而不是点 (sort -k1.4)。点指定字段中的一个字符,而我猜您想对前 4 个字段进行排序。
    • @AlexStylianos 同意。我已经相应地编辑了答案。
    猜你喜欢
    • 1970-01-01
    • 2016-01-20
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    相关资源
    最近更新 更多