How to Check Out a File from Another Branch in Git
Short answer
To bring a single file from another branch into your working directory without switching branches, run git checkout <branch-name> -- path/to/file (or git restore --source=<branch-name> path/to/file in Git 2.23+). This overwrites the file in your working directory with the version from the specified branch and stages the change immediately. You can then review the change with git diff --cached and commit it to permanently incorporate that file version into your current branch's history. This is useful when you want to cherry-pick a single configuration file, template, or script from another branch without dragging in all of that branch's other commits. The operation overwrites the current file without prompting, so double-check the branch name and file path before running it.
Have you ever been working on a feature branch and realized you need a specific file from another branch, like main? Maybe you deleted a file by accident and want to restore it from the main branch, or perhaps you need a utility script from a colleague's feature branch.
Your first thought might be to stash your changes, check out the other branch, copy the file, switch back, and then apply your stash.
That works… but there's a much faster and more elegant way!
The git checkout Command for a Single File
You can use the git checkout command to pluck a single file from any other branch and bring it directly into your current working directory.
The command structure looks like this:
git checkout <branch-name> -- <path/to/file>
Let's break that down:
git checkout: The command you're using.<branch-name>: The name of the branch where the file you want exists (e.g.,main,develop,feature/new-login).--: This is a crucial separator. It tells Git that you're done with options and branch names, and what follows is a file path. This prevents confusion if you have a file with the same name as a branch.<path/to/file>: The exact path to the file you want to check out.
The Git Cheat Sheet
No need to remember all Git commands and parameters: get our popular "Git Cheat Sheet" - for free!
Free. We email you a ZIP with our cheat sheets.
A Practical Example
Imagine you are on a branch called feature/add-user-profile and you accidentally deleted the utils/helpers.js file. You know the file exists on the main branch and you want to restore it.
Instead of switching branches, you can simply run:
Careful: this command can discard work.
git checkout main -- utils/helpers.js
And just like that, the utils/helpers.js file from the main branch will appear in your working directory, ready to be staged and committed. It's that simple!
Checking Out a File from a Specific Commit
You can take this even further. What if you don't want the latest version of the file, but a version from a specific commit? No problem! Just replace the branch name with the commit hash:
# You can find the commit hash using 'git log'
$ git log
commit a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0
Author: Your Name <you@example.com>
Date: Tue Jul 1 10:00:00 2025 +0200
Add amazing new feature
# Now, check out the file from that specific commit
$ git checkout a1b2c3d4 -- utils/helpers.js
This powerful feature allows you to restore any file to any state it has ever been in throughout your project's history.
No Command Line Needed
Checking Out Files in Tower
In case you are using the Tower Git client, you can simply click on any commit from any branch and select "Show [FILE] at Revision [REVISION]" to view the full content of that specific version of the file.
You can also restore a file to the "Working Copy" view, making that version available regardless of which branch you're currently working on. Simply click on "Restore [FILE] at Revision [REVISION] in Working Copy…" from the context menu.
Final Thoughts
Using git checkout to grab a single file is a fantastic tool to have in your Git toolbox. It saves you time and context-switching, allowing you to stay focused on your current branch while easily pulling in files from elsewhere in your repository.
It's a safe, efficient, and precise way to manage your files across different branches and commits.