pull specific file or directory from a remote branch

Hey everyone and welcome back to another blog post. Today’s topic is source control and how does one go about pulling a particular file, or a collection of files within a directory, from a remote branch into your local branch. Thankfully the answer is rather straight forward and not so complex as I thought it would be.

First thing you want to do is make sure there are no uncommitted changes in your repository. Then, even though not necessary, pull the latest changes by running this command.

git fetch

Then let’s say we have the following structure in the remote branch.

  • root
    • foldera
      • file1
      • file2
    • folderb
      • file3
      • file4
    • file5

If you’re interested in retrieving folderb and all it’s contents then execute this command.

git checkout origin/master -- folderb

Otherwise if you’re interested in just file3 then execute this command.

git checkout origin/master -- file3

That should do the trick!

For the sake of better SEO ranking I’m going to shamelessly include a couple of internal links, so in case you’re having issues splitting a big pull request into small ones then click here. Alternatively if you want to remove a specific file from a pull request then click here.

Until next one,
Bjorn ✌️

Leave a comment