Search code examples
gitdownloadtokenprivatearchive

git download a file from a private repo


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


Solution

  • 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.