7

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?

Community
  • 1
  • 1
user740006
  • 1,759
  • 4
  • 20
  • 30
  • 5
    You can get a list of valid paths int he commit with `git ls-tree -r --name-only abcd123` –  Jun 08 '15 at 14:31
  • @WumpusQ.Wumbley Thanks a lot. It works. I can view the file I need now. In the above example the path "subdirA/subdirB/file1.txt" should be correct, but I probably have typed the path wrongly so that I got an error message. – user740006 Jun 08 '15 at 14:37
  • @WumpusQ.Wumbley this is worth writing an answer ) – Nick Volynkin Jun 09 '15 at 17:28
  • amazing question but you can explain why you want to see the files from the a older commit? – simon Mar 11 '20 at 17:31

1 Answers1

4

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.