Git
version control
merging branches
file management
software development

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:

bash
git switch main
git restore --source feature-branch -- path/to/file.txt

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:

bash
git restore --source feature-branch -- src/app.js config/settings.yml

After that, inspect the changes and commit them:

bash
1git status
2git diff
3git add src/app.js config/settings.yml
4git commit -m "Bring selected files from feature-branch"

The Older Equivalent: git checkout branch -- path

Older Git tutorials often show:

bash
git checkout feature-branch -- path/to/file.txt

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:

bash
1git switch release
2git restore --source feature/payment-refactor -- pom.xml
3git diff
4git add pom.xml
5git commit -m "Copy pom.xml from payment refactor branch"

That leaves the rest of feature/payment-refactor untouched.

Entire Directories Work Too

You can restore directories, not just single files:

bash
git restore --source feature-branch -- src/components/

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:

bash
git add src/components/

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 -- path is the clearest modern form.'
  • Older Git versions may use git checkout branch -- path instead.
  • 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.

Course illustration
Course illustration

All Rights Reserved.