How to get a file from another branch in Git
By Pull This RepoPosted 3 min read
Short answer
To git checkout files from another branch, stay on your branch and run git checkout other-branch path/to/file. Git replaces your copy with that branch's version and stages it. On Git 2.23 or later, git restore --source=other-branch path/to/file does the same without staging. For a remote branch, run git fetch first and use origin/branch-name.
You can git checkout files from another branch without switching to it or merging anything. Stay on your own branch, name the other branch and the file, and Git copies that version over yours. There are two commands for it: the long-standing git checkout <branch> <file> (often written with a -- separator before the file), and git restore --source, added in Git 2.23. This page shows both, how to copy a whole folder, how to grab a file from a remote branch, and how to keep a side-by-side copy instead of overwriting. Syntax was checked against the Git docs on git-scm.com on 22 November 2026.
How do you copy a file from another branch with git checkout?
Run it from the branch that should receive the file:
git checkout feature src/config.jsThe git checkout docs say this form will "replace the specified files and/or directories with the version from the given commit or tree and add them to the index". In plain words: your src/config.js becomes the copy from feature, and it is already staged. Run git status to see it under "Changes to be committed", then commit:
git commit -m "Take config.js from feature"Many guides write this with a double dash, --, between the branch and the file. The git checkout docs say Git treats an ambiguous name as a branch, and -- forces it to read the rest as file paths. Add it when a file has the same name as a branch, or when Git answers ambiguous argument.
How do you use git restore --source instead?
Use --source with the branch, then the path:
git restore --source=feature src/config.jsThe git restore docs say --source restores "the working tree files with the content from the given tree", and that it is common to name a branch, commit or tag there. Without --staged, only the file on disk changes; it is not staged. To change both the file and the staging area, like git checkout does:
git restore --source=feature --staged --worktree src/config.jsThe short flags are -s, -S and -W, so git restore -s feature -SW src/config.js is the same command.
Which one should you use?
Either works. The table shows how they differ:
git checkout feature path | git restore --source=feature path | |
|---|---|---|
| Available since | every Git version | Git 2.23 |
| Updates your file | yes | yes |
| Stages the change | yes | only with --staged |
Files missing from feature | left in place (overlay mode) | removed to match (no-overlay mode) |
The last row matters for folders, covered next. git restore exists because, as the Git 2.23.0 release notes put it, git switch and git restore were introduced to split checking out branches and checking out paths "out of the single git checkout command".
How do you get a whole folder from another branch?
Give the folder as the path:
git checkout feature src/components/
# or
git restore --source=feature src/components/Here the two commands behave differently. The git checkout docs say that in its default overlay mode it "never removes files from the index or the working tree". So a file you have in src/components/ that does not exist on feature stays. The git restore docs say its default is no-overlay mode, which removes tracked files that are not in the source "to make them match" it exactly. Pick git restore when you want the folder to be an exact copy, and git checkout when you only want to bring files in. git checkout --no-overlay feature src/components/ gives you the exact-copy behaviour with checkout too.
How do you get a file from a remote branch?
Fetch first, then use the remote-tracking name:
git fetch origin
git checkout origin/feature src/config.js
# or
git restore --source=origin/feature src/config.jsgit fetch downloads the latest commits from GitHub without touching your files. After that, origin/feature names that branch as it is on the remote, and you can take files from it like any local branch. No local feature branch is needed.
How do you get a file from a specific commit?
Use a commit ID, tag or relative name instead of a branch:
git checkout a1b2c3d README.md
git restore --source=v1.2.0 README.md
git restore --source=main~2 MakefileThe git restore docs use that last example: it puts Makefile back to how it was two commits before the tip of main.
How do you look at the other version without overwriting yours?
Use git show with branch:path:
# print it in the terminal
git show feature:src/config.js
# save it beside yours under a new name
git show feature:src/config.js > config-from-feature.jsThe git show docs give the form next~10:Documentation/README as showing a file's contents at that point in a branch. To see what would change before you copy, compare the two versions of the file directly:
git diff main:src/config.js feature:src/config.jsWhat is the catch?
Copying a file overwrites your version with no merge, so uncommitted edits to that file are lost. Commit or stash them first; our git stash guide shows how. Copying one file also brings none of the history from the other branch: the new commit simply records the new contents. If you staged the copy by mistake, how to unstage a file in Git undoes it, and the Git cheat sheet has the rest of the branch commands on one page.
Pull it. Stash it.
Questions people ask
How do I copy a file from another branch without switching branches?
Run git checkout other-branch path/to/file from the branch you are on. Git copies that branch's version of the file into your working folder and stages it, and you never leave your current branch. Commit it when you are happy with the change.
What is the difference between git checkout with a branch and file, and git restore --source?
git checkout with a branch and a path updates both your working folder and the staging area, so the file is ready to commit. git restore --source changes only the working folder unless you add --staged. They also treat deleted files differently when you restore a whole folder.
How do I get a file from a remote branch?
Run git fetch origin so your copy knows the latest remote commits, then use the remote-tracking name, for example git checkout origin/feature config.yml or git restore --source=origin/feature config.yml. You do not need to create a local branch first.
Can I get a file from another branch without overwriting mine?
Yes. Use git show with the branch and path joined by a colon and send the output to a new file name, for example git show feature:app.js > app-feature.js. Your own app.js stays as it is, and you can compare the two copies before choosing.
Sources
More repos like this
Get tomorrow's repo in your feed
One free, open source repo a day as a short video, shown working, with the catch named. Pull it. Stash it.
