Skip to content
Start » git » Git quick tip: git checkout specific files from another branch

Git quick tip: git checkout specific files from another branch

The command git checkout can be used to update specific files or directories in your working tree with those in another branch (branch) without having to merge the entire branch.

This can be useful when working with multiple feature branches (feature braches) or use GitHub Pages to generate a static project site.

the page of git-checkout manual describes how the git checkout command is not just useful for switching between branches.

When <paths> or --patch are provided, git checkout no change branch. It updates the named paths in the working tree from the index file or a call <tree-ish> (usually a commit)…The argument <tree-ish> can be used to specify a specific tree-ish (ie commit, tag or tree) to update the index to the given paths before updating the working tree.

in git, a tree-ish is a way of referring to a particular commit or tree. This can be a sha partial or the branch, remote, and tag name pointers.

The syntax for using the git checkout to update the working tree with files from a tree-ish is as follows:

git checkout [-p|--patch] [ ] [--] ...

So, 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 --

For example, you could update a script on a branch of a new feature being developed by “pulling” the specific file from the main branch.

# On branch main git checkout new-branch # On new-branch git checkout main -- my_script.js git commit -m "Update my_script.js from main"

It is interesting to read the manual page of the git-checkout to know the other possibilities that the command brings, and understand its versatility to deal with specific issues in your versioning flow and source code management of software development projects.

Leave a Reply

Your email address will not be published.

%d bloggers like this: