How to Download a Single File from Gitlab

How to download a single file from GitLab?

Gitlab has a rest API for that.

You can GET a file from repository with curl:

curl https://gitlab.com/api/v4/projects/:id/repository/files/:filename\?ref\=:ref

For example:

curl https://gitlab.com/api/v4/projects/12949323/repository/files/.gitignore\?ref\=master

If your repository isn't public you also need to provide an access token by adding --header 'Private-Token: <your_access_token>'.



Links:

You can check how to find repository api id here.

Api documentation

More on tokens

There is also a python library that uses this api.

Note that this is GitLab specific solution and won't work for other hostings.

how to download single folder OR file in gitlab repository

Is there any way to clone a git repository's sub-directory only?

Use a sparse checkout, available since version 1.7.0.

How to download single raw file from private gitlab repo using username and password

To download a repository file through curl, you need to use the repository files API endpoint.

Using your example https://gitlab.com/athos.oc/lodash-has-plugin/-/raw/master/.eslintrc.json, would turn into:

https://gitlab.com/api/v4/projects/30349314/repository/files/%2Eeslintrc%2Ejson/raw?ref=master

However, API authentication does not include username and password as an available authentication method, so you would need to use a token (or a session cookie).

Download single files from GitHub

Git does not support downloading parts of the repository. You have to download all of it. But you should be able to do this with GitHub.

When you view a file it has a link to the "raw" version. The URL is constructed like so

https://raw.githubusercontent.com/user/repository/branch/filename

By filling in the blanks in the URL, you can use Wget or cURL (with the -L option, see below) or whatever to download a single file. Again, you won't get any of the nice version control features used by Git by doing this.

Update: I noticed you mention this doesn't work for binary files. You probably shouldn't use binary files in your Git repository, but GitHub has a download section for each repository that you can use to upload files. If you need more than one binary, you can use a .zip file.
The URL to download an uploaded file is:

https://github.com/downloads/user/repository/filename

Note that the URLs given above, from the links on github.com, will redirect to raw.githubusercontent.com. You should not directly use the URL given by this HTTP 302 redirect because, per RFC 2616: "Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests."

Download a single file from GitLab artifacts archive

The gitlab's documentation should be improved. You need to urlencode the artifact path and the project id:

$ export TOKEN="12345678"
$ export GITLAB_HOST="gitlab.example.com"
$ export PROJECT_ID="foo"
$ export URLENCODED_PROJECT_ID="foo"
$ export JOB_NAME="bundle"
$ export FILE_PATH="build/ui-bundle.zip"
$ export URLENCODED_FILE_PATH="build%2Fui-bundle.zip"
$ curl --header "PRIVATE-TOKEN: $TOKEN"\
"https://$GITLAB_HOST/api/v4/projects/$URLENCODED_PROJECT_ID/jobs/artifacts/master/raw/$URLENCODED_FILE_PATH?job=$JOB_NAME"

This answer has a bash implementation for urlencode function.

using api gitlab to download file

When following the docs, you need to follow the /projects/:id/repository/files/:file_path format for the link, so don't replace everything, just the pieces with : in front.

You also need to make sure that the file path is using URI encoding.

With the information that you have, it would look something like this:

https://gitlabxxxxxxxxxx/api/v4/projects/98/repository/files/src%2Fmain%2Fscript%2Fdeploiement%2Fsettings%2Frci%2Fsetting.sh?ref=master

Cannot download file from gitlab using command line

For such case, it's better to use Gitlab API and specially Repository File API to get file from repository :

curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.corp.mycompany.com/api/v4/projects/<project_id>/repository/files/my_wanted_file?ref=master"

Create your personal token following these instructions.

How to sparsely checkout only one single file from a git repository?

Originally, I mentioned in 2012 git archive (see Jared Forsyth's answer and Robert Knight's answer), since git1.7.9.5 (March 2012), Paul Brannan's answer:

git archive --format=tar --remote=origin HEAD:path/to/directory -- filename | tar -O -xf -

But: in 2013, that was no longer possible for remote https://github.com URLs.

See the old page "Can I archive a repository?"

The current (2018) page "About archiving content and data on GitHub" recommends using third-party services like GHTorrent or GH Archive.


So you can also deal with local copies/clone:

You could alternatively do the following if you have a local copy of the bare repository as mentioned in this answer,

git --no-pager --git-dir /path/to/bar/repo.git show branch:path/to/file >file

Or you must clone first the repo, meaning you get the full history:
- in the .git repo
- in the working tree.

  • But then you can do a sparse checkout (if you are using Git1.7+),:

    • enable the sparse checkout option (git config core.sparsecheckout true)
    • adding what you want to see in the .git/info/sparse-checkout file
    • re-reading the working tree to only display what you need

To re-read the working tree:

$ git read-tree -m -u HEAD

That way, you end up with a working tree including precisely what you want (even if it is only one file)


Richard Gomes points (in the comments) to "How do I clone, fetch or sparse checkout a single directory or a list of directories from git repository?"

A bash function which avoids downloading the history, which retrieves a single branch and which retrieves a list of files or directories you need.



Related Topics



Leave a reply



Submit