有时候不是想翻整个仓库历史,只想知道某个文件最近是谁改过、改了哪几次。这个场景直接把路径放到 -- 后面就行。
环境:Git 2.30+。可以在空目录里试一下:
mkdir git-file-log-demo && cd git-file-log-demo
git init
printf 'v1\n' > note.txt
git add note.txt
git commit -m 'init note'
printf 'v2\n' >> note.txt
git add note.txt
git commit -m 'update note'
printf 'other\n' > other.txt
git add other.txt
git commit -m 'add other'
git log --oneline -- note.txt
最后那条 add other 不会出现在结果里,因为它没碰 note.txt。
路径前面的 -- 主要是把“参数”和“文件路径”隔开,文件名刚好像分支名或参数时会少点歧义。平时排查某个配置文件、脚本、迁移文件的变动,我觉得这个比先开图形工具更快一点。