Rafid
Rafid

Reputation: 20269

GIT: Checkout to a "Really" Specific Folder

I want to export, checkout, or whatever you call it from the index, HEAD, or any other commit, to a specific folder, how is that possible? Similar questions have already been asked:

GIT: Checkout to a specific folder

Do a "git export" (like "svn export")?

But the problem with the proposed solution is that they preserve the relative path. So for example, if I use the mentioned method to check out the file nbapp/editblog.php to the folder temp, the file would be checked out in temp/nbapp/editblog.php!

Is there anyway to checkout to 'temp' directly?

Also, another important thing is to be able to check the HEAD or any other commit. The checkout-index (which allows using the --prefix option to checkout to a specific folder, while normal checkout doesn't allow) checks out only the index. What if I want to check out a file from a certain commit to a certain folder? A similar question has alread

Upvotes: 0

Views: 612

Answers (3)

Martin
Martin

Reputation: 551

Use the --work-tree

This forces you to also use --git-dir to point to your .git directory. --git-dir=.git assumes that your are in your git folder. You can of course invoke this command from anywhere else, by adjusting this folder.

And then specify a <tree> instead of a commit. This removes the leading pathes.

git  --git-dir=.git  --work-tree=/folder/into/which/to/export/  restore  --source=HEAD:path/in/your/repo  .

Using :path/in/your/repo should use the index.

If you only want one file, then name the file instead of using a "." at the end.

If you want part of the folder structure (for the sub-path) to be created in the dest folder

git  --git-dir=.git  --work-tree=/folder/into/which/to/export/  restore  --source=HEAD:path/in/  your/repo

Still only exports path/in/your/repo. But creates the directory /folder/into/which/to/export/your/repo.

Upvotes: 0

CB Bailey
CB Bailey

Reputation: 793319

With git archive, you can include the path elements that you don't want to replicate in the specification of the treeish. E.g.

git archive --prefix=temp/ HEAD:nbapp editblog.php | tar x

creates temp/editblog.php, whereas

git archive --prefix=temp/ HEAD nbapp/editblog.php | tar x

creates temp/nbapp/editblog.php.

Upvotes: 1

wRAR
wRAR

Reputation: 25569

git cat-file blob revision:path > path/where/to/save/file

Upvotes: 2

Related Questions