Git show's file path

24.5k views Asked by At

Let's say I have a git repo for my stuffs inside a directory myapp:

myapp
    /.git
    /.gitignore
    /subdirA
        /subdirB
            /file1.txt
            /file2.txt
            /(and some other stuffs)
    /(and some other stuffs)

I want to view file1.txt from an old commit abcd123. For another thread, I learnt that I should use something like:

$ git show abcd123:path/to/file1.txt

However, I am confused about the right path to use. I've tried something like

$ git show abcd123:myapp/subdirA/subdirB/file1.txt
$ git show abcd123:subdirA/subdirB/file1.txt
$ git show abcd123:subdirB/file1.txt
$ git show abcd123:file1.txt

but git keeps giving me error messages like

$ Path 'subdirA/subdirB/file1.txt' does not exist in 'abcd123'

How to solve this problem?

1

There are 1 answers

0
AudioBubble On BEST ANSWER

This command will generate a list of paths that exist in comment abcd123:

git ls-tree -r --name-only abcd123

Anything you get from that command should work as a path for git show abcd123:...

Also, it's easier sometimes to use a leading ./ on the path. That automatically replaces the . with the path from the repository root to your current directory in the working tree. For example:

cd ~/my-git-repo/dir1/dir2
git show abcd123:./Makefile # equivalent to git show abcd123:dir1/dir2/Makefile

If you try git show abcd123:Makefile it doesn't work... but git does suggest ("Did you mean...?") both the version with the full path from the repository root and the version with the ./ ... unless you also have a Makefile in the root directory of abcd123 in which case you just get that with no warning that you might have wanted ./Makefile instead.