0

I would like to write a git tree into a directory, ie given that output from git ls-tree:

% git ls-tree -lrt 312735a86fe300861ebf0e6b081616029c2f6b76 

040000 tree ba37a4590a12acdd773e444b06e75ad151150ed2       -    subdir1
100644 blob deba01fc8d98200761c46eb139f11ac244cf6eb5      10    subdir1/test.txt
100644 blob dce813842fd90177bd0fbfd3011797934ee31303      15    test2.txt

I want to do:

% git some-command 312735a86fe300861ebf0e6b081616029c2f6b76 /tmp/output-dir

And have in output-dir:

tree output-dir
.
├── subdir1
│   └── test.txt
└── test2.txt

I imagine I could parse output of git ls-tree and then, based on the second column call git ls-tree recursively or git cat-file. My question is more to know if there is a built-in tool (or an external tool) which allows to do just so?

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
fanf42
  • 1,828
  • 15
  • 22
  • Does `git checkout 312735a86...` work? Once you have the checkout you can move it to a directory of your choice. You might also experiment with `--work-tree` (and/or `--git-dir`) see https://stackoverflow.com/a/1386350/1671066. – Micha Wiedenmann Jul 03 '18 at 12:00

1 Answers1

1

You can use git archive to export the desired tree as a .tar archive (it sends it by default to stdout) and pipe its output through tar -x to unpack the files into the desired directory:

git archive HEAD -- subdir1 | tar -x -C /tmp/output-dir

Replace subdir1 with the path inside the repository of the directory you want to export.

axiac
  • 68,258
  • 9
  • 99
  • 134