我目前正在从事的基于 Git 的项目,我通常总是在一个子目录中。
下面是我从存储库根目录的子目录运行命令 git log --name-only
时的输出。
commit 678bd5ba6fc5474c4c61406768bf6cba5937c5d1
Author: thegreendroid
Date: Mon Mar 27 09:36:24 2017 +1300
Commit message
child_dir1_from_root/file1 | 184 +--
child_dir2_from_root/file2 | 2 +-
如何让 git log
输出如下所示的内容?这使得比较列出的文件非常容易,只需复制文件路径并运行 git diff HEAD~ {copied_file_path}
而不必手动修改文件路径然后运行命令。
commit 678bd5ba6fc5474c4c61406768bf6cba5937c5d1
Author: thegreendroid
Date: Mon Mar 27 09:36:24 2017 +1300
Commit message
file1 | 184 +--
../child_dir2_from_root/file2 | 2 +-
我查看了 git log
文档,但没有什么特别之处。我可以编写脚本来执行此操作,但我很好奇 Git 是否有内置方法。
最佳答案
为了在 git log --name-only
的输出中使用路径,将选项 --git-dir
添加到 git diff
.
git --git-dir="$(git rev-parse --show-toplevel)"/.git diff HEAD~ child_dir1_from_root/file1
为了方便使用,在配置中做一个别名。
[alias]
mdiff = "! git --git-dir=\"$(git rev-parse --show-toplevel)\"/.git diff"
git mdiff HEAD~ child_dir1_from_root/file1
现在只要当前目录属于工作树就可以工作。
关于git - 如何让 git log 显示相对于当前目录的文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43750693/