-1

I need to download single file (script) from git repository. I have followed this article and manage to download file that is in root folder of the project (repository). But unfortunately, I can't figure out how to checkout file that is inside directory.

My question is how to specify path to file that is inside directory when checking out (downloading) single file? (e.g. fileA.txt from folderA in example below)

Repository example:

|-folderA
  |-fileA.txt
  |-fileB.txt
|-fileC.txt
|-fileD.txt

Git commands that I have run on my PC:

git clone --branch develop -n https://bitbucket.host/.../my_repository.git    // clone repository with no files
cd my_repository      // navigate to repository folder
git fetch
git checkout HEAD fileD.txt        // successfully downloads fileD.txt file
git checkout HEAD fileC.txt        // successfully downloads fileC.txt file
git checkout HEAD "folderA/fileA.txt"   // error: pathspec 'folderA/fileA.txt' did not match any file(s) known to git
git checkout HEAD "/folderA/fileA.txt"   // fatal: Invalid path '/folderA': No such file or directory 
czmafi00
  • 31
  • 5
  • 2
    `git checkout HEAD -- folderA/fileA.txt` perhaps – matt Oct 19 '21 at 11:42
  • It worked, thanks! What is the logic behind it? Why '--'? – czmafi00 Oct 19 '21 at 12:10
  • Cool, I'll give it as an answer. – matt Oct 19 '21 at 12:41
  • Note: Git does not deal in *files*, but rather in entire *commits*. When you clone a repository, you get *all of the commits*. Each commit contains *all of the files*. So by the time you have a clone, you have downloaded *every version of every file*. The `git checkout` step means *extract one commit* or *extract one particular file from one particular commit*. There's no downloading involved at this point. – torek Oct 19 '21 at 13:52

1 Answers1

1

Try saying it this way:

git checkout HEAD -- folderA/fileA.txt

(For the meaning of the double dashes, see Stack Overflow questions such as Meaning of Git checkout double dashes)

matt
  • 515,959
  • 87
  • 875
  • 1,141