How to merge specific files from Git branches
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Sometimes you do not want to merge an entire branch. You only want one or two files from it. In Git, the usual solution is to stay on the target branch and restore or check out specific paths from the source branch, then review and commit those file changes normally.
The Modern Command: git restore
If you are on the branch that should receive the file, use:
This copies the version of path/to/file.txt from feature-branch into your working tree on main.
If you want to bring in several files:
After that, inspect the changes and commit them:
The Older Equivalent: git checkout branch -- path
Older Git tutorials often show:
This still works in many environments, but git restore is easier to read because it clearly expresses that you are restoring file content rather than switching branches.
If your team uses older Git versions, the checkout form may still be necessary.
Restoring a File to the Exact Other-Branch Version
This operation replaces the current file content with the version from the source branch. It does not perform a content-level merge for that file. If you need to combine changes manually, you have two options:
- restore the file and then edit it
- use a normal branch merge and resolve only the files you care about
Be clear about which one you want. "Take that exact file version" and "merge both file histories" are different actions.
Example Workflow
Suppose you are on release and want only pom.xml from feature/payment-refactor:
That leaves the rest of feature/payment-refactor untouched.
Entire Directories Work Too
You can restore directories, not just single files:
This is useful when one coherent part of the tree is ready but the rest of the branch is not.
If you want to stage the restored content immediately, add the path after reviewing it:
Common Pitfalls
The most common mistake is running the command while checked out on the wrong branch. Always switch to the branch that should receive the file first.
Another issue is assuming this records a branch merge relationship. It does not. Git sees it as file content copied into the current branch and then committed there.
A third pitfall is forgetting to review the diff. Pulling a file from another branch may also bring in assumptions about related files, configuration, or dependencies that are not present on the target branch.
Finally, if the file is already modified locally, restoring from another branch can overwrite your uncommitted work. Check git status first.
Summary
- To bring specific files from another branch, stay on the target branch and restore those paths from the source branch.
- '
git restore --source branch -- pathis the clearest modern form.' - Older Git versions may use
git checkout branch -- pathinstead. - This copies file content; it is not the same as merging whole branch history.
- Review and commit the selected changes like any other working-tree modification.

