Search code examples
gitrelative-path

Is it possible to 'git restore --work-tree.. ' a file with a relative filename?


I am trying to restore a file to a different directory given the file's relative filename:

git --work-tree some/directory restore -s HEAD ./numbers.txt

Yet, this command fails with error: pathspec './numbers.txt' did not match any file(s) known to git.

I am a bit puzzled because git restore, without the --work-tree option does not fail:

git restore -s HEAD ./numbers.txt

So, is it possible to use git --work-tree dir restore with a relative filename?


Solution

  • What's giving you trouble is that when you specify an explicit work tree all paths are interpreted relative to that work tree, . isn't relative to your current directory any more, it's that work tree. So Git's looking for a toplevel numbers.txt.

    When you just want to put specific content there without really checking it out, use git show. Here, git show @:./numbers.txt >some/directory/numbers.txt.