Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloning single file from Git repository

Tags:

git

I wanted to clone a sub directory https://github.com/CoreyMSchafer/code_snippets/tree/master/Django_Blog/12-Password-Reset/django_project

from the parent directory

https://github.com/CoreyMSchafer/code_snippets.git

I have gone through some Stack Overflow answers and they say that Git is not designed to download specific files from root folder.

I tried below commands in my cmd

git clone https://github.com/CoreyMSchafer/code_snippets.git -b code_snippets/tree/master/Django_Blog/12-Password-Reset/django_project but it did not work out.

if this might be a possible duplicate question.

like image 291
Anoop K George Avatar asked Dec 04 '25 05:12

Anoop K George


1 Answers

There is a difference between cloning the whole repo and downloading. When you say cloning, this means that you're interested in all the history, meaning what happened to the file as the repository has evolved. I don't think its possible with git because its not designed to do so. I'll be glad to be proven otherwise though.

If you want, however, to "just download" the last "snapshot" of the file (which I assume what you really want), then you have a couple of options:

  1. Use git archive command:
git archive --remote=ssh://<address>/repo.git <BranchName|HEAD> /some/path/file.txt | tar -xO /some/path/file.txt > /tmp/file.txt
  1. Its possible that you have some HTTP access to git repository and can use wget to download the file.

Read This thread in SO for more information/ ideas

like image 169
Mark Bramnik Avatar answered Dec 06 '25 21:12

Mark Bramnik