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?

4
  • 5
    You can get a list of valid paths int he commit with git ls-tree -r --name-only abcd123 Commented Jun 8, 2015 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. Commented Jun 8, 2015 at 14:37
  • @WumpusQ.Wumbley this is worth writing an answer ) Commented Jun 9, 2015 at 17:28
  • amazing question but you can explain why you want to see the files from the a older commit? Commented Mar 11, 2020 at 17:31

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.