Home > Back-end >  git download a file from a private repo
git download a file from a private repo

Time:04-20

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

CodePudding user response:

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

CodePudding user response:

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.

  • Related