Git: How to pull a single file from a server repository in Git?
Solution 1
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
Solution 2
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.
Solution 3
This can be the solution:
git fetch
git checkout origin/master -- FolderPathName/fileName
Thanks.
Solution 4
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).
Solution 5
This scenario comes up when you -- or forces greater than you -- have mangled a file in your local repo and you just want to restore a fresh copy of the latest version of it from the repo. Simply deleting the file with /bin/rm (not git rm) or renaming/hiding it and then issuing a git pull
will not work: git notices the file's absence and assumes you probably want it gone from the repo (git diff
will show all lines deleted from the missing file).
git pull
not restoring locally missing files has always frustrated me about git, perhaps since I have been influenced by other version control systems (e.g. svn update which I believe will restore files that have been locally hidden).
git reset --hard HEAD
is an alternative way to restore the file of interest as it throws away any uncommitted changes you have. However, as noted here, git reset is is a potentially dangerous command if you have any other uncommitted changes that you care about.
The git fetch ... git checkout
strategy noted above by @chrismillah is a nice surgical way to restore the file in question.
Related videos on Youtube
Comments
-
vsvs almost 3 years
I am working on a site with a server running Git. I am using Git for deployment (not GitHub). This was set up prior to my involvement using a hook method, and I referred to this question and entered the commands below, but it didn't work.
How do I pull a single file from the server? For instance, if I wanted to update my local file index.php?
git pull index.php
?-
Mateusz Piotrowski about 8 yearsPossible duplicate of Is it possible to pull just one file in Git?
-
Ciro Santilli OurBigBook.com about 8 yearsPossible duplicate of How to checkout only one file from git repository?
-
-
vsvs about 9 yearsThank you. So what do you mean by
<revision>
? the file name? And if my file is in the root directory would that mean i have to type:git checkout -m index.php index.php
? -
vsvs about 9 yearsThank you for the explanation.
-
Bernhard Döbler over 6 yearsDoes it hast to be
*origin*/master
or can it be from any remote? Is the whole history pulled into my repo or does the file seem to appear magically? -
Eduard over 6 yearsIf you get this error "did not match any file(s) known to git": "path/to/file" should not be the copy of the path that you get from the file location on GitHub, meaning "repoName/fileName", you should get rid of "repoName/" and then it will work.
-
chrismillah almost 6 years@BernhardDöbler it can be any branch from remote :)
-
chrismillah almost 6 years@Eduard that is correct, it is your local path-- i would recommend using relative as you stated. You can use
pwd
in unix/linux systems to 'print working directory'. this will show you where you are for absolute paths -
Roberto Novakosky over 5 yearsAttention: "git checkout master -- index.php" this dont do checkout from server, but from local git database from last pull.
-
Thecave3 about 5 yearsway much esplicative than others comments. Thank you
-
Zeitounator almost 5 yearsThe OP specifically indicated in the question that he is not using github.
-
Rajanikanta Pradhan over 4 yearsHi I am new to git, I tried with this it's working fine. But I have a doubt, git fetch command will fetch the changes from remote but will not merge the changes to working copy. But while using fetch command and checkout command(the solution you provided) how the changes are merged to the working copy, as we have not hit any merge command. I know that checkout command to switch the branch. need your help @chrismillah
-
chrismillah over 4 years@RajanikantaPradhan with that specific command for checkout, you are checking out a specific file into your working directory, not anything else. Does that help?
-
Admin almost 4 yearsHow make it renamed once.. what git command line of it?
-
alper almost 3 yearsWhy we are not doing
git fetch --all
instead ofgit fetch
? -
chrismillah almost 3 yearsGood call @alper
-
ThiagoYB over 2 yearsSimple and efficient, thanks
-
Legioneroff over 2 yearsThanks, dude! By me works it:
git checkout origin/main -- name_of_file