How to make git log show file paths relative to current directory?
Asked Answered
E

2

8

The 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.

Exoenzyme answered 3/5, 2017 at 3:45 Comment(0)
G
6

In order to use the paths in the output of git log --name-only, add the option --git-dir to git 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.

Gormley answered 3/5, 2017 at 7:29 Comment(1)
Beautiful! The alias method works fine, but directly running it on the command line throws an error (fatal: Not a git repository: '$(git rev-parse --show-toplevel)/.git').Exoenzyme
K
2

You can add up the --relative='PATH' to the subdirectory you want.

Kymric answered 3/5, 2017 at 4:35 Comment(1)
Well this appears to exclude changes apart from the specified directory, which is not what I want.Exoenzyme

© 2022 - 2024 — McMap. All rights reserved.