180

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?

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
vsvs
  • 1,975
  • 2
  • 12
  • 14
  • 1
    Possible duplicate of [Is it possible to pull just one file in Git?](http://stackoverflow.com/questions/16230838/is-it-possible-to-pull-just-one-file-in-git) – Mateusz Piotrowski Jan 31 '16 at 13:14
  • Possible duplicate of [How to checkout only one file from git repository?](http://stackoverflow.com/questions/2466735/how-to-checkout-only-one-file-from-git-repository) – Ciro Santilli OurBigBook.com Mar 30 '16 at 10:49

10 Answers10

295

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 git@github.com: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
chrismillah
  • 3,704
  • 2
  • 14
  • 20
  • 1
    Thank you. So what do you mean by ``? 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 Feb 06 '15 at 22:04
  • Does 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? – Bernhard Döbler Sep 23 '17 at 16:09
  • If 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. – Eduard Oct 20 '17 at 09:48
  • 1
    @BernhardDöbler it can be any branch from remote :) – chrismillah May 14 '18 at 19:32
  • @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 – chrismillah Jun 12 '18 at 01:07
  • Hi 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 – Rajanikanta Pradhan Aug 13 '19 at 19:52
  • @RajanikantaPradhan with that specific command for checkout, you are checking out a specific file into your working directory, not anything else. Does that help? – chrismillah Aug 20 '19 at 21:16
  • How make it renamed once.. what git command line of it? –  Jun 25 '20 at 06:37
  • 1
    Why we are not doing `git fetch --all` instead of `git fetch`? – alper May 05 '21 at 21:24
  • Good call @alper – chrismillah May 26 '21 at 19:07
  • Thanks, dude! By me works it: `git checkout origin/main -- name_of_file` – Legioneroff Jan 06 '22 at 12:13
40
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.

OYORF
  • 433
  • 1
  • 5
  • 10
22

This can be the solution:

git fetch

git checkout origin/master -- FolderPathName/fileName

Thanks.

Y. Joy Ch. Singha
  • 3,056
  • 24
  • 26
14

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

jno
  • 997
  • 1
  • 10
  • 18
7

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.

Trutane
  • 1,279
  • 12
  • 12
2

Try using:

git checkout branchName -- fileName

Ex:

git checkout master -- index.php
simhumileco
  • 31,877
  • 16
  • 137
  • 115
Dodda Venkata
  • 61
  • 1
  • 7
1

just do git checkout / -- path/to/file

Ex.: git checkout origin/staging -- src/main.ts

VyTor BrB
  • 81
  • 8
0

This windows batch works regardless of whether or not it's on GitHub. I'm using it because it shows some stark caveats. You'll notice that the operation is slow and traversing hundreds of megabytes of data, so don't use this method if your requirements are based on available bandwidth/R-W memory.

sparse_checkout.bat

pushd "%~dp0"
if not exist .\ms-server-essentials-docs mkdir .\ms-server-essentials-docs
pushd .\ms-server-essentials-docs
git init
git remote add origin -f https://github.com/MicrosoftDocs/windowsserverdocs.git
git config core.sparseCheckout true
(echo EssentialsDocs)>>.git\info\sparse-checkout
git pull origin master

=>

C:\Users\user name\Desktop>sparse_checkout.bat

C:\Users\user name\Desktop>pushd "C:\Users\user name\Desktop\"

C:\Users\user name\Desktop>if not exist .\ms-server-essentials-docs mkdir .\ms-server-essentials-docs

C:\Users\user name\Desktop>pushd .\ms-server-essentials-docs

C:\Users\user name\Desktop\ms-server-essentials-docs>git init Initialized empty Git repository in C:/Users/user name/Desktop/ms-server-essentials-docs/.git/

C:\Users\user name\Desktop\ms-server-essentials-docs>git remote add origin -f https://github.com/MicrosoftDocs/windowsserverdocs.git Updating origin remote: Enumerating objects: 97, done. remote: Counting objects: 100% (97/97), done. remote: Compressing objects: 100% (44/44), done. remote: Total 145517 (delta 63), reused 76 (delta 53), pack-reused 145420 Receiving objects: 100% (145517/145517), 751.33 MiB | 32.06 MiB/s, done. Resolving deltas: 100% (102110/102110), done. From https://github.com/MicrosoftDocs/windowsserverdocs * [new branch]
1106-conflict -> origin/1106-conflict * [new branch]
FromPrivateRepo -> origin/FromPrivateRepo * [new branch]
PR183 -> origin/PR183 * [new branch]
conflictfix -> origin/conflictfix * [new branch]
eross-msft-patch-1 -> origin/eross-msft-patch-1 * [new branch]
master -> origin/master * [new branch] patch-1
-> origin/patch-1 * [new branch] repo_sync_working_branch -> origin/repo_sync_working_branch * [new branch]
shortpatti-patch-1 -> origin/shortpatti-patch-1 * [new branch]
shortpatti-patch-2 -> origin/shortpatti-patch-2 * [new branch]
shortpatti-patch-3 -> origin/shortpatti-patch-3 * [new branch]
shortpatti-patch-4 -> origin/shortpatti-patch-4 * [new branch]
shortpatti-patch-5 -> origin/shortpatti-patch-5 * [new branch]
shortpatti-patch-6 -> origin/shortpatti-patch-6 * [new branch]
shortpatti-patch-7 -> origin/shortpatti-patch-7 * [new branch]
shortpatti-patch-8 -> origin/shortpatti-patch-8

C:\Users\user name\Desktop\ms-server-essentials-docs>git config core.sparseCheckout true

C:\Users\user name\Desktop\ms-server-essentials-docs>(echo EssentialsDocs ) 1>>.git\info\sparse-checkout

C:\Users\user name\Desktop\ms-server-essentials-docs>git pull origin master
From https://github.com/MicrosoftDocs/windowsserverdocs
* branch master -> FETCH_HEAD

kayleeFrye_onDeck
  • 6,648
  • 5
  • 69
  • 80
0

Now is possible and easy

git archive --remote=git://git.organization.com/repo.git HEAD:path/in/repo filename | tar -x
Jorge Tovar
  • 1,374
  • 12
  • 17
-1
https://raw.githubusercontent.com/[USER-NAME]/[REPOSITORY-NAME]/[BRANCH-NAME]/[FILE-PATH]

Ex. https://raw.githubusercontent.com/vipinbihari/apana-result/master/index.php

Through this you would get the contents of an individual file as a row text. You can download that text with wget.

Ex. https://raw.githubusercontent.com/vipinbihari/apana-result/master/index.php