Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get specific files from a GIT remote branch

Tags:

git

Is it possible to pull from a remote repository but only selectively take files from that remote that I'm interested in? I don't want to simply pull down the entire branch.

Thanks.

like image 608
Martin Blore Avatar asked Aug 20 '12 15:08

Martin Blore


People also ask

Can I pull a specific file from git?

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


1 Answers

A "remote branch" is nothing more than a commit pointer and the affiliated pack data. Just git fetch <remote> and then if you want to view diffs between files on the remote and your local, you can do so with:

git diff <local_branch> <remote>/<remote_branch> -- <file>

This would in many cases be, for example, git diff master origin/master -- <file>. You could also see the commit differences with git log:

git log <local_branch>..<remote>/<remote_branch> -- <file>

so... git log master..origin/master -- <file>

Finally if you just want to checkout a particular version of a file from the remote (this wouldn't be ideal; much better to merge the remote branch with git merge <remote>/<remote_branch> or git pull), use:

git checkout <remote>/<remote_branch> -- <file>
like image 199
Christopher Avatar answered Oct 18 '22 22:10

Christopher