Every developer has faced this moment: you are deep into a feature branch, and suddenly you realize that a configuration file, a utility function, or an entire stylesheet exists in another branch—perhaps main, develop, or a colleague's experimental branch—and you need it right now without switching your entire working context. Knowing how to git get file from another branch is one of those practical version-control skills that saves time, prevents unnecessary merges, and keeps your commit history clean. Whether you are using an older version of Git or the latest release, When it comes to this, several reliable ways stand out Worth keeping that in mind..
Why You Might Need to Get a File from Another Branch
Before jumping into the commands, it helps to understand the common scenarios where this task becomes necessary. You might need to:
- Recover a file that was deleted in your current branch but still exists in
main. - Copy a shared configuration file, such as
.eslintrcordocker-compose.yml, from a stable branch into your feature work. - Compare an implementation from an experimental branch without merging the entire branch.
- Apply a hotfix from a release branch to your development branch without bringing along unrelated changes.
- Retrieve a file that a teammate pushed to a remote branch while you are working locally.
In each case, the goal is the same: bring specific file content into your current branch while leaving the rest of the branch's history untouched And it works..
Method 1: Using git checkout (The Classic Approach)
The most traditional way to git get file from another branch is with the git checkout command. Despite its name, checkout can be used to restore individual files, not just switch branches.
Syntax
git checkout --
Example
Imagine you are on a branch called feature/login-page, and you need the database.js file from the develop branch:
git checkout develop -- src/config/database.js
After running this command, Git copies the version of database.js from develop into your current working directory and stages it in the index. You can then review the changes and commit them when ready.
Important Notes
- The double dash (
--) separates the branch name from the file path. This prevents confusion if a branch name and a file path look similar. - This command updates both your working directory and the staging area, so the file will appear under "Changes to be committed" when you run
git status. - If you are using Git version 2.23 or newer,
git checkoutfor files is still supported, but Git now encourages splitting its responsibilities intogit switchandgit restore.
Method 2: Using git restore (The Modern Approach)
Starting with Git 2.23, the git restore command was introduced to make file restoration more intuitive. It is now the recommended way to git get file from another branch in
modern workflows. Unlike checkout, which handles both branch switching and file restoration, restore has a single, clear purpose: restoring working tree files Not complicated — just consistent. Practical, not theoretical..
Syntax
git restore --source= --
Or, using the short flag:
git restore -s --
Example
Staying on feature/login-page and pulling database.js from develop:
git restore -s develop -- src/config/database.js
Key Differences from checkout
- Unstaged by Default:
git restoreupdates your working directory only. The file will appear as "Changes not staged for commit" ingit status. This is often safer, as it forces you to explicitly review andgit addthe changes before committing. - Staging Option: If you want the behavior of
checkout(staging the file automatically), add the--staged(or-S) flag:git restore -s develop -S -- src/config/database.js - Explicit Intent: The flags (
-sfor source,-Sfor staged) make the command self-documenting, reducing the cognitive load compared to remembering the positional arguments ofcheckout.
Method 3: Using git show (For Inspection or Piping)
If you want to view the file content before applying it, or pipe it directly into another command (like diff or a text editor), git show is the tool. It outputs the file content to stdout without touching your working directory And it works..
Quick note before moving on.
Syntax
git show :
Example
git show develop:src/config/database.js
Practical Use Cases
- Compare differences:
git show develop:src/config/database.js | diff - src/config/database.js - Save to a new filename:
git show develop:src/config/database.js > src/config/database.backup.js - Inspect without checkout: Quickly verify this is the version you actually want before running a restore command.
Method 4: Fetching from a Remote Branch Directly
If the branch you need the file from exists only on the remote (e.g., origin) and you haven't fetched it locally yet, you don't strictly need to create a local tracking branch. You can reference the remote branch directly after a fetch Less friction, more output..
Workflow
- Ensure your remote references are up to date:
git fetch origin - Use the remote ref in any of the commands above:
# Using restore (modern) git restore -s origin/main -- .github/workflows/ci.yml # Using checkout (classic) git checkout origin/main -- .github/workflows/ci.yml # Using show (inspect) git show origin/main:.github/workflows/ci.yml
This is particularly useful in CI/CD pipelines or when reviewing a colleague's branch that you haven't checked out locally Less friction, more output..
Method 5: git archive (For Bulk or Scripted Extraction)
While overkill for a single file, git archive is excellent for scripting the extraction of multiple specific files or entire directory structures from a specific tree-ish (commit, branch, tag) without a working checkout.
Example
Extract only the config directory from main into the current folder:
git archive --remote=. main config/ | tar -x
(Note: --remote=. works locally; for a remote server, replace . with the repo URL, though many hosted Git providers disable upload-archive for security.)
Common Pitfalls and How to Avoid Them
1. Pathspec Errors
Error: error: pathspec 'src/config/database.js' did not match any file(s) known to git
Cause: The file does not exist on the source branch, or the path is relative to the repository root, not your current directory.
Fix: Always provide the path relative to the repository root. Run git ls-tree -r <branch> --name-only | grep <filename> to verify the exact path on the source branch.
2. Overwriting Uncommitted Changes
Both checkout and restore will silently overwrite uncommitted changes in your working directory for that specific file.
Fix: Commit or stash your local changes first:
git stash
git restore -s develop -- src/config/database.js
git stash pop
3. Submodules
If the path points to a submodule, restore/checkout updates the submodule pointer (the commit hash), not the contents of the submodule itself. You must cd into the submodule and run the command there to retrieve internal files.
Summary Cheat Sheet
| Command | Git Version | Stages Changes? | Best For |
|---|---|---|---|
git checkout <branch> -- <file> |
All | Yes | Muscle memory; quick staging. |
| `git restore -s <branch> -- |
This is where a lot of people lose the thread.