Copy a single file from local Git repository

git

I need to copy a SINGLE FILE from LOCAL REPOSITORY to my machine, not git-pull or git-fetch, how can i do it?

Is it possible to get it via hash ?
such as a3ea2118bf1c5e2c6aa0974d0b6ff7415bd044ef ?

Best Answer

You can use git archive to obtain a single file from a repository:

git archive --remote=file:///path/to/repository.git HEAD:path/to/directory filename | tar -x

The repository specified as --remote can be local, remote, bare or regular, it works in all of the aforementioned cases.

Note that if you want to obtain a version of filename from a specific commit, you can replace HEAD in the oneliner above with the hash of the desired commit.

Related Question