Git FAQ
Frequently asked questions around Git and Version Control.
Git FAQ featured image

How to Check Out a File from Another Branch in Git

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!

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:

$ 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.


Tip

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.

About Us

As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.

Just like with Tower, our mission with this platform is to help people become better professionals.

That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.