1

I want to download a file from a private repo. I want to do this with "git" (not gitlab/github api).

Git provide this way https://git-scm.com/docs/git-archive. You can fetch a file like this

git archive --remote=ssh://host/pathto/repo.git HEAD README.md

However I cannot use ssh. I want to download file using http and a token I have.

I can clone my repo like this:

git clone -b branch http://oauth2:$gitToken@${gitRepo}

but if u make a command like this:

git archive --remote=http://oauth2:$gitToken@${gitRepo} branch filename 

You encounter an error:

fatal: operation not supported by protocol

So the question is how we can download a file from a private git repo using a token. (git-archive approach or other (git) way)

Thanks

mnr
  • 83
  • 7
  • 2
    Does this answer your question? [git archive fatal: Operation not supported by protocol](https://stackoverflow.com/questions/11258599/git-archive-fatal-operation-not-supported-by-protocol) – Ôrel Apr 19 '22 at 07:28
  • 1
    Ty for comment. I'm afraid it does not. I'm using git v2.25.1, that is rather new version. Also I want to use token in request because it is a private repo. – mnr Apr 19 '22 at 11:36
  • 1
    Git doesn't really download *files*, but rather downloads *commits*. See [VonC's answer](https://stackoverflow.com/a/71921698/1256452) for ways to make Git download fewer commits than usual. When you use `git archive` you're not invoking Git as usual, which is why you have to use the ssh method to do this with GitHub. Other sites may have their own special rules. – torek Apr 19 '22 at 17:03

3 Answers3

2

GitHub (and I suspect most public Git hosting service provider) does not support remote archiving through HTTPS.

The more modern git way would be the recent git clone --filter/sparse-checkout approach

git clone --filter=blob:none --sparse https://[token]@github.com/CSSEGISandData/COVID-19.git
cd COVID-19/
git sparse-checkout init --cone
git sparse-checkout add /the folder you want/

See also this approach, where I edit .git\info\sparse-checkout

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Ty for Answering. In sparse-checkout u can get a specific directory. Its not for getting a specific file. Am I right? And for limitation in public Git hosts u mentioned, can it be solved in personal git host? Then how u can add token to request? – mnr Apr 19 '22 at 11:41
  • @mnr Yes to the first: that would limit the number of files to one folder. I am not sure regarding the second question: I suppose that indeed you can add support for remote archive on a Git server you control, but I do not know (yet) how. – VonC Apr 19 '22 at 12:07
  • Thank u for helping – mnr Apr 20 '22 at 04:23
  • @mnr Don't forget to accept an answer for this or your past question. You can [accept your own answer](https://stackoverflow.com/help/self-answer, starting tomorrow). – VonC Apr 20 '22 at 06:01
2

I have modified previous answer and have got working script for downloading only one file with read access token

 git clone --depth 1 --filter=blob:none \
  -b master https://<my_token_name>:<my_token>@<my_git_repo>.git tmp_dir && \
  cd tmp_dir && \
  git sparse-checkout set <path_to_file_in_repo> && \
  git checkout master && \
  test -f <path_to_file_in_repo>
Dmitriy Grachev
  • 359
  • 2
  • 4
1

I was hoping there was a straightforward solution to this. But the only way I could achieve downloading files was "sparse-checkout" as they said.

However I modified the way to get only a file, not a directory so it differ from that instruction.

For downloading file u can do this:

git clone --depth 1 --filter=blob:none --no-checkout -b branchName http://oauth2:$gitToken@$gitRepo repoFolder
cd repoFolder
git sparse-checkout set $fileName

Those git clone flags are for minimizing getting extra files.

And this will fetch your file in that directory.

mnr
  • 83
  • 7