git checkout command can be used to update specific files or directories in your working tree with those from another branch, without merging in the whole branch. This can be useful when working with several feature branches or using GitHub Pages to generate a static project site.
git-checkout manual page describes how the
git checkout command is not just useful for switching between branches.
When <paths> or
git checkoutdoes not switch branches. It updates the named paths in the working tree from the index file or from a named <tree-ish> (most often a commit)…The
argument can be used to specify a specific tree-ish (i.e. commit, tag or tree) to update the index for the given paths before updating the working tree.
In git, a ‘tree-ish’ is a way of referring to a particular commit or tree (Read more about ‘tree-ish’ in the Git Community Book). This can be a partial sha or the branch, remote, and tag name pointers.
The syntax for using
git checkout to update the working tree with files from a tree-ish is as follows:
git checkout [-p|--patch] [<tree-ish>] [--] <pathspec>…
Therefore, to update the working tree with files or directories from another branch, you can use the branch name pointer in the
git checkout command.
git checkout <branch_name> -- <paths>
As an example, this is how you could update your
gh-pages branch on GitHub (used to generate a static site for your project) to include the latest changes made to a file that is on the master branch.
# On branch master git checkout gh-pages git checkout master -- myplugin.js git commit -m "Update myplugin.js from master"
The need to update my gh-pages branch with specific files from my master branch was how I first found out about the other uses of the
checkout command. It’s worth having a read of the rest of the
git-checkout manual page and experimenting with the options.
Comment on this post
Please wrap code fragments in
<code> tags, wrap blocks of code in
<pre><code>, and use JsFiddle to post working examples.