Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How to pull a single file from a server repository in Git?

People also ask

How do I download individual files from repository?

Downloading From The Command Line You will need to create a personal access token to use the API, with which you can replace “ACCESS_TOKEN” in this script. You can then use a JSON parser like jq to pull out the URL and download it. To get the repository file list recursively, you will need to fetch the root tree.

How do I download a specific file from a GitHub repository?

If it's just a single file, you can go to your GitHub repo, find the file in question, click on it, and then click “View Raw”, “Download” or similar to obtain a raw/downloaded copy of the file and then manually transfer it to your target server.


Short Answer

It is possible to do (in the deployed repository):

git fetch --all
// git fetch will download all the recent changes, but it will not put it in your current checked out code (working area).

Followed by:

git checkout origin/master -- path/to/file
// git checkout <local repo name (default is origin)>/<branch name> -- path/to/file will checkout the particular file from the downloaded changes (origin/master).

Full Example

$ cd /project/directory

$ git branch
* develop

check the remote name

$ git remote -v
origin [email protected]:abc/123.git

Confirmed it's origin and

I am on branch develop and need a file from branch main

File i need is src/scss/main.scss

git fetch --all
git checkout origin/main -- src/scss/main.scss

git fetch --all
git checkout origin/master -- <your_file_path>
git add <your_file_path>
git commit -m "<your_file_name> updated"

This is assuming you are pulling the file from origin/master.


This can be the solution:

git fetch

git checkout origin/master -- FolderPathName/fileName

Thanks.


I was looking for slightly different task, but this looks like what you want:

git archive --remote=$REPO_URL HEAD:$DIR_NAME -- $FILE_NAME |
tar xO > /where/you/want/to/have.it

I mean, if you want to fetch path/to/file.xz, you will set DIR_NAME to path/to and FILE_NAME to file.xz. So, you'll end up with something like

git archive --remote=$REPO_URL HEAD:path/to -- file.xz |
tar xO > /where/you/want/to/have.it

And nobody keeps you from any other form of unpacking instead of tar xO of course (It was me who need a pipe here, yeah).