使用git查看与子目录中的特定文件扩展名相关的所有日志

hec*_*tor 11 git git-log

我试图查看存储库历史记录中的提交,但仅针对具有特定扩展名的文件.

如果这是目录结构:

$ tree
.
??? a.txt
??? b
??? subdir
    ??? c.txt
    ??? d
Run Code Online (Sandbox Code Playgroud)

这是完整的历史:

$ git log --name-only --oneline 
a166980 4
subdir/d
1a1eec6 3
subdir/c.txt
bc6a027 2
b
f8d4414 1
a.txt
Run Code Online (Sandbox Code Playgroud)

如果我想查看扩展名为.txt的文件的日志:

$ git log --oneline *.txt
f8d4414 1
Run Code Online (Sandbox Code Playgroud)

它仅返回当前目录中的文件,而不返回子目录中的文件.我想在当前目录中包含所有可能的子目录.

我也尝试过:

$ git log --oneline */*.txt
1a1eec6 3
Run Code Online (Sandbox Code Playgroud)

和:

$ git log --oneline *.txt */*.txt
1a1eec6 3
f8d4414 1
Run Code Online (Sandbox Code Playgroud)

这适用于这种情况,但对于更通用的情况则不实用.

和:

$ git log --oneline HEAD -- *.txt
f8d4414 1
Run Code Online (Sandbox Code Playgroud)

没有成功.

sfl*_*che 15

尝试:

git log --oneline -- '*.txt'
Run Code Online (Sandbox Code Playgroud)

--用于指示只定位参数也会随之而来.并'*.txt'从当前目录中搜索所有文件夹.

或者,您可以通过在子目录中开始搜索来限制结果,例如-- 'sub/*.txt'.

  • 如果没有引号,它会在 git 看到它之前被你的 shell 展开。 (3认同)
  • shell 会将 `*.txt` 扩展为当前工作目录中的文件,除非没有要扩展的文件,在这种情况下 shell 将传递 `*.txt` 作为参数。放置“*.txt”意味着 shell 不会扩展参数。这就是为什么有时单引号是不必要的,但有时却是必要的。 (2认同)