Overview
Sometimes you only need a few files from another branch — not the entire merge.
For example, maybe you added a utility or fixed a small bug on one branch and now want to copy just that file into your main branch.
Here’s how you can do that safely and cleanly using Git.
Scenario
You’re currently on your main branch:
git branch --all
* main
feature-branch
A commit on the feature-branch added or updated a few files that you want to bring into main.
You don’t want to merge the entire branch or cherry-pick the whole commit — just copy specific files.
Checkout Files from Another Branch
To copy specific files from another branch without switching branches:
git checkout feature-branch -- path/to/file1 path/to/file2
This pulls the versions of file1 and file2 from the feature-branch into your working directory on main.
Then stage and commit the files:
git add .
git commit -m "Imported specific files from another branch"
Works for New Files Too
If a file doesn’t exist on your current branch but exists on the other branch, Git will still copy it into your working directory. It will appear as a new, untracked file until you stage it:
git add path/to/newfile
git commit -m "Added new file from another branch"
Checkout Files from a Specific Commit
If you want to copy files from a specific commit rather than the latest branch state, use the commit hash:
git checkout <commit-hash> -- path/to/file
Example:
git checkout abcd1234 -- src/utils/helper.ts
This retrieves the version of the file exactly as it existed in that commit.
Partial Cherry-Pick (Optional)
If you want to apply a commit’s changes but commit only certain files:
git cherry-pick -n <commit-hash>
The -n flag applies the commit without committing it.
You can then unstage or restore unwanted files and commit the ones you need:
git restore unwanted/file.js
git add desired/file1 desired/file2
git commit -m "Partially cherry-picked commit abcd1234"
Summary
| Goal | Command |
|---|---|
| Copy file(s) from another branch | git checkout branch-name -- path/to/file |
| Copy file(s) from a specific commit | git checkout commit-hash -- path/to/file |
| Apply commit but only keep certain files | git cherry-pick -n commit-hash |
Finding File Paths
If you’re not sure where the file is located in another branch, list all tracked files and filter by name:
git ls-tree -r branch-name --name-only | grep filename
This helps confirm the exact path before checking out.
Conclusion
This method is ideal when you need to move or copy specific files between branches without merging. It keeps your history clean, prevents unnecessary conflicts, and gives you precise control over what you bring into your branch.
Next time you only need one or two files from another branch, skip the merge and use git checkout instead.