5

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.

Anoop K George
  • 1,605
  • 12
  • 40
  • 1
    Duplicate of https://stackoverflow.com/questions/2466735/how-to-sparsely-checkout-only-one-single-file-from-a-git-repository – blami May 04 '20 at 07:39
  • Does this answer your question? [How to sparsely checkout only one single file from a git repository?](https://stackoverflow.com/questions/2466735/how-to-sparsely-checkout-only-one-single-file-from-a-git-repository) – phd May 04 '20 at 13:25

2 Answers2

9

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

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
0

If your Git repository remote hosting service used Git 2.38+ (Q3 2022), it would implement a "get" capability:

See commit 65da938 (23 Aug 2022), and commit e21e663, commit 59c1752, commit 5556891, commit 53a5089, commit b5624a4 (09 Aug 2022) by Derrick Stolee (derrickstolee).
(Merged by Junio C Hamano -- gitster -- in commit 68ef042, 01 Sep 2022)

remote-curl: add 'get' capability

Reviewed-by: Josh Steadmon
Signed-off-by: Derrick Stolee

A future change will want a way to download a file over HTTP(S) using the simplest of download mechanisms.
We do not want to assume that the server on the other side understands anything about the Git protocol but could be a simple static web server.

Create the new 'get' capability for the remote helpers which advertises that the 'get' command is avalable.
A caller can send a line containing 'get <url> <path>' to download the file at <url> into the file at <path>.

gitremote-helpers now includes in its man page:

'get'

Can use the 'get' command to download a file from a given URI.

gitremote-helpers now includes in its man page:

'get' <uri> <path>

Downloads the file from the given <uri> to the given <path>.

If <path>.temp exists, then Git assumes that the .temp file is a partial download from a previous attempt and will resume the download from that position.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I couldnt get it working , do you have a working example of the get command ? – EngineerAkki Apr 21 '23 at 11:58
  • 1
    @EngineerAkki You need a remote service which supports that `get` call, and this is not yet the case. As explained [in this thread](https://public-inbox.org/git/220801.86zggogh5j.gmgdl@evledraar.gmail.com/T/#med193fdcc1b7eff2ac6e7f5a8f651b39a99dde0b), adding `get` is for supporting `git clone --bundle-uri=`, and the "`bundle-uri`" I [detailed here](https://stackoverflow.com/a/73671473/6309) is still a "work in progress". – VonC Apr 21 '23 at 12:34