Cloning single file from Git repository
Asked Answered
L

3

10

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.

Lyle answered 4/5, 2020 at 7:27 Comment(2)
Duplicate of #2467235Invention
Does this answer your question? How to sparsely checkout only one single file from a git repository?Indiscerptible
P
14

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

Pounds answered 4/5, 2020 at 7:45 Comment(0)
D
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.

Darton answered 11/9, 2022 at 16:59 Comment(2)
I couldnt get it working , do you have a working example of the get command ?Refusal
@Refusal You need a remote service which supports that get call, and this is not yet the case. As explained in this thread, adding get is for supporting git clone --bundle-uri=<X>, and the "bundle-uri" I detailed here is still a "work in progress".Darton
P
0

As mentioned by Mark Bramnik, you can use git archive to download the current snapshot. However, if you do want to track the changes to some of the files/sub-directries, you can use sparse checkouts.

According to Cloning a single file in Git:

  1. init a git repo locally
git init
  1. add the remote repo (the source to "download" from)
git remote add origin <remote-uri>
  1. enable sparse checkout
git config core.sparseCheckout true
  1. add the path to files to sparse checkout to
.git/info/sparse-checkout
  1. checkout (the specified file(s)) from the remote repo
git fetch origin main
git checkout main
  • note that main is just the conventional name for the branch, the repo in question may name it differently and/or you may want to track another branch (such as a release branch)
Phipps answered 15/8 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.