You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否用单个Git命令列出多个文件的最新提交ID?

一次性获取多个文件的最新提交ID

你可以通过shell循环一次性处理所有目标文件,每个文件单独执行git log来获取它的最新提交记录,以下是具体实现:

直接执行的命令

for file in file1 file2 file3; do
    printf "%-10s " "$file"
    git log -1 --pretty=format:"%h  %aD" -- "$file"
    echo
done

执行后会输出类似你期望的格式:

file1      456fac4  Sat, 06 Jan 2024 00:39:39 +0000
file2      7ac3621  Mon, 25 Dec 2023 10:17:59 +0000
file3      69deba2  Sat, 07 Jan 2024 23:09:23 +0000

封装成便捷函数

如果需要频繁使用,可以把这段逻辑封装成shell函数,添加到你的~/.bashrc或~/.zshrc中:

git-last-commit() {
    if [ $# -eq 0 ]; then
        echo "Usage: git-last-commit <file1> <file2> ..."
        return 1
    fi
    for file in "$@"; do
        if [ -f "$file" ]; then
            commit_info=$(git log -1 --pretty=format:"%h  %aD" -- "$file")
            printf "%-10s %s\n" "$file" "$commit_info"
        else
            printf "%-10s File not found\n" "$file"
        fi
    done
}

保存后重启终端,之后就可以像你期望的那样直接调用:

git-last-commit file1 file2 file3

命令参数说明

  • %h:输出短格式的提交ID(和示例中的格式一致)
  • %aD:输出作者提交时的日期时间,格式和示例匹配
  • 循环中的printf用来对齐文件名,让输出更整齐

你之前用git log -1 -- file1 file2 file3无效的原因是:这个命令是查找包含所有指定文件的最新单次提交,而不是每个文件各自的最后一次修改提交。

内容的提问来源于stack exchange,提问作者GOVarney

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 14:46:01