# git download

4 messages from 2022-11-23 to 2022-11-25. Participants: Vasilij Demyanov, Jacob Keller, Sean Allred, ZheNing Hu.
Thread: https://gitlist.dev/t/58846

## Vasilij Demyanov, 2022-11-23 17:17

Subject: git download
Message-ID: <CACtGy4i7Jv+UyjwKOLsMOQwUO81=o98AA5SNwi+=xUB76ehD_g@mail.gmail.com>
URL: https://gitlist.dev/e/CACtGy4i7Jv%2BUyjwKOLsMOQwUO81%3Do98AA5SNwi%2B%3DxUB76ehD_g%40mail.gmail.com

```
Hello everybody!

I have a need to get just one file from a repository, it would be
useful to have a command something like this:

git download repo_url branch_or_commit path/to/file

Or maybe there is something like this, I just haven't found it in the docs?

Best regards,
Vasyl

```

## Jacob Keller, 2022-11-23 19:14

Subject: Re: git download
Message-ID: <CA+P7+xoAh3bFBEvVcRF8Xb-DxxWbbM=R0gWVQ6JsFCWtHy4SDw@mail.gmail.com>
URL: https://gitlist.dev/e/CA%2BP7%2BxoAh3bFBEvVcRF8Xb-DxxWbbM%3DR0gWVQ6JsFCWtHy4SDw%40mail.gmail.com
In-Reply-To: <CACtGy4i7Jv+UyjwKOLsMOQwUO81=o98AA5SNwi+=xUB76ehD_g@mail.gmail.com>

```
On Wed, Nov 23, 2022 at 9:27 AM Vasilij Demyanov <qvasic@gmail.com> wrote:
>
> Hello everybody!
>

Hi,

> I have a need to get just one file from a repository, it would be
> useful to have a command something like this:
>

Ok.

> git download repo_url branch_or_commit path/to/file
>
> Or maybe there is something like this, I just haven't found it in the docs?
>

So this isn't something git itself provides. Various web hosting
services for git provide such access, because they allow viewing the
repository contents. Git can't provide this easily because of the
nature of the commits and trees used to store the contents of the
repository. In order to verify that you got the correct contents, you
essentially have to fetch the commit and all its parents. You can
maybe save some download space by using a sparse clone.. but most
remote repositories may not allow arbitrary commit hash fetching.

If you already have a repository available from some computer/system
you can extract the file at a given version via something like git
cat-file:

$ git cat-file -p <commitish>:<path>

where commitish can be a branch or a commit hash, or other way to
reference a commit, and path is the path of the file.

Unfortunately you will essentially need to have cloned the repository first.

If your project is hosted somewhere using software like github,
gitlab, etc, or has gitweb-like software operating on it, you may be
able to find an API or web interface that lets you extract the
individual file without downloading the repository. This is somewhat
outside the scope of this mailing list tho.

Thanks,
Jake

> Best regards,
> Vasyl

```

## Sean Allred, 2022-11-24 16:52

Subject: Re: git download
Message-ID: <87zgcgs2ne.fsf@gmail.com>
URL: https://gitlist.dev/e/87zgcgs2ne.fsf%40gmail.com
In-Reply-To: <CACtGy4i7Jv+UyjwKOLsMOQwUO81=o98AA5SNwi+=xUB76ehD_g@mail.gmail.com>

```

Vasilij Demyanov <qvasic@gmail.com> writes:

> I have a need to get just one file from a repository, it would be
> useful to have a command something like this:
>
> git download repo_url branch_or_commit path/to/file

My immediate thought was git-archive[1], but depending the specifics of
your needs/setup, you could use curl/wget for this pretty simply. For
example, if you're using GitHub, you can

    curl https://raw.githubusercontent.com/git/git/master/README.md

to dump the content of the README to standard out. As another example,
to get the content of next:builtin/var.c,

    curl https://raw.githubusercontent.com/git/git/next/builtin/var.c

The general pattern:

    https://raw.githubusercontent.com/<repo>/<branch-or-commit>/<file-path>

GitLab's pattern is similar:

    https://gitlab.com/<repo>/-/raw/<branch-or-commit>/<file-path>

e.g.

    curl https://gitlab.com/gitlab-org/git/-/raw/main/README.md

This will be limited to getting a single file at a time. If you want
more, you can make more curl requests, but git-archive can give a whole
directory or even the entire repo at a specific commit. Since
git-archive outputs a tar/zip file, you'll want to combine it with your
favorite decompression tool to get your content.

[1]: https://git-scm.com/docs/git-archive

--
Sean Allred

```

## ZheNing Hu, 2022-11-25 06:06

Subject: Re: git download
Message-ID: <CAOLTT8QF6gqYJf79VQOFn64YD+tew2R2MxjOArm8LZJXC6YbJw@mail.gmail.com>
URL: https://gitlist.dev/e/CAOLTT8QF6gqYJf79VQOFn64YD%2Btew2R2MxjOArm8LZJXC6YbJw%40mail.gmail.com
In-Reply-To: <CACtGy4i7Jv+UyjwKOLsMOQwUO81=o98AA5SNwi+=xUB76ehD_g@mail.gmail.com>

```
Hi,

Vasilij Demyanov <qvasic@gmail.com> 于2022年11月24日周四 01:27写道：
>
> Hello everybody!
>
> I have a need to get just one file from a repository, it would be
> useful to have a command something like this:
>
> git download repo_url branch_or_commit path/to/file
>
> Or maybe there is something like this, I just haven't found it in the docs?
>

If the git server support partial-clone (uploadpack.allowFilter),
maybe you can use this way:

    git clone --depth=1 --single-branch --branch=main --no-checkout
--filter=blob:none
git@github.com:derrickstolee/sparse-checkout-example.git
    cd sparse-checkout-example
    git sparse-checkout set --no-cone "/README.md"
    git checkout main

it will only download single commit, multiple tree, and one blob.

If the server supports fetch any objects
(uploadpack.allowAnySHA1InWant) and you already know the
file's SHA1, there is a kind of black magic here:

    git init repo
    cd repo
    git remote add origin <repo>
    git -c fetch.negotiationAlgorithm=noop fetch force --no-tags
--no-write-fetch-head --filter=blob:none --stdin
    <blob-oid>

You will get only one blob, "git cat-file -p <blob-oid>" to view the
contents of this file.

Note these two performance maybe not better than git archive or some
file pages support by git server
directly such as
https://raw.githubusercontent.com/derrickstolee/sparse-checkout-example/main/README.md

> Best regards,
> Vasyl

--
ZheNing Hu

```
