如何使git日志显示相对于当前目录的文件路径? [英] How to make git log show file paths relative to current directory?
问题描述
下面是当我运行命令
时的输出,从存储库根目录下的git log --name-only 。 提交678bd5ba6fc5474c4c61406768bf6cba5937c5d1
作者:thegreendroid
日期:星期一3月27日09:36:24 2017 +1300
提交消息
child_dir1_from_root / file1 | 184 + -
child_dir2_from_root / file2 | 2 + -
我如何获得 git log 来输出类似下面呢?通过复制文件路径并运行 git diff HEAD〜{copied_file_path} ,而不必手动修改文件路径,然后运行命令,这使得列出的文件变得非常简单。
提交678bd5ba6fc5474c4c61406768bf6cba5937c5d1
作者:thegreendroid
日期:星期一3月27日09:36:24 2017 +1300
提交消息
file1 | 184 + -
../child_dir2_from_root/file2 | 2 + -
我已经看过git log文档,但没有什么突出的。我可以编写一个脚本来做到这一点,但我很好奇Git是否有内置的方式?
为了使用在 git log --name-only
输出中的路径,将选项 - git-dir
添加到 git diff
。git --git-dir =$(git为了简单起见,我们使用了一个简单的例子来说明如何使用git diff HEAD来解决这个问题。在配置中使用别名。
现在可以工作。[别名]
mdiff =!git --git-dir = \$(git rev-parse --show-toplevel)\/ .git diff
只要当前目录属于工作树,git mdiff HEAD〜child_dir1_from_root / file1The current Git-based project that I am working on, I am generally always in a sub-directory.
Below is the output when I run the command git log --name-only from a sub-directory of the root of the repository.
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 +-
How do I instead get git log to output something like below instead? This makes diffing the files listed really easy, by just copying the file path and running git diff HEAD~ {copied_file_path} rather than having to modify the file path manually and then run the command.
commit 678bd5ba6fc5474c4c61406768bf6cba5937c5d1 Author: thegreendroid Date: Mon Mar 27 09:36:24 2017 +1300 Commit message file1 | 184 +-- ../child_dir2_from_root/file2 | 2 +-
I have looked at the git log documentation but nothing stood out. I can write a script to do this but I was curious whether Git has a built-in way?
解决方案In order to use the paths in the output of
git log --name-only
, add the option--git-dir
togit diff
.git --git-dir="$(git rev-parse --show-toplevel)"/.git diff HEAD~ child_dir1_from_root/file1
For easy use, make an alias in the config.
[alias] mdiff = "! git --git-dir=\"$(git rev-parse --show-toplevel)\"/.git diff"
git mdiff HEAD~ child_dir1_from_root/file1
now can work as long as the current directory belongs to the working tree.这篇关于如何使git日志显示相对于当前目录的文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!