Git Checkout One File From Another Branch

5 min read

How to Git Checkout One File from Another Branch: A Step‑by‑Step Guide

When you’re collaborating on a project, it’s common to need a single file from a different branch without moving the entire workspace. Whether you’re borrowing a critical fix, retrieving a template, or simply comparing code, git checkout one file from another branch lets you do exactly that. This article walks you through the process, explains the underlying concepts, and offers tips for handling common issues. By the end, you’ll be comfortable using the command git checkout <branch> -- <file> (or its modern equivalents) to manage files across branches efficiently.

Introduction

In Git, a branch is a lightweight pointer to a specific commit, allowing you to work on parallel versions of your codebase. Now, this operation is often called “checking out a file from another branch. Occasionally, you may want to bring a single file from one branch into your current working directory without merging the whole branch. ” It’s useful for quickly copying a configuration file, a migration script, or any piece of code that you need right away. The main keyword for this guide is git checkout one file from another branch, and we’ll explore how to achieve this using both classic and newer Git syntax.

Not obvious, but once you see it — you'll see it everywhere Not complicated — just consistent..

Understanding the Basics

Before diving into the commands, it’s helpful to understand what “checkout” means in Git terminology. Even so, when you checkout a file, you replace its contents with the version stored at a particular commit (in this case, a commit on a different branch). This operation updates the working tree and the staging area, but it does not affect the history of the current branch Still holds up..

Key concepts to keep in mind:

  • HEAD: The tip of the current branch.
  • Branch pointer: A reference like feature/login that points to a specific commit.
  • Working directory: Where you edit files.
  • Staging area: Where changes are prepared for a commit.

When you run git checkout <branch> -- <file>, Git performs the following behind the scenes:

  1. It looks up the commit that <branch> points to.
  2. It extracts the version of <file> from that commit.
  3. It replaces the current file in your working directory with that version.
  4. It optionally stages the change (depending on the flags used).

Step‑by‑Step Guide

Below is a clear, numbered sequence that you can follow in any repository. And all examples assume you are currently on the main branch and want to copy config. yaml from the dev branch.

1. Verify Your Current Branch and Remote Status

git status
git branch --show-current
  • git status shows whether any files are already staged or modified.
  • git branch --show-current confirms you’re on the intended branch (e.g., main).

2. Ensure the Target Branch Exists Locally

If the branch you need (dev) is only on the remote, fetch it first:

git fetch origin dev

Then create a local tracking branch (optional but recommended):

git checkout -b dev origin/dev

Now you have a local dev branch you can reference.

3. Use the Classic Checkout Syntax

The most straightforward way to git checkout one file from another branch is:

git checkout dev -- config.yaml
  • dev is the source branch.
  • -- tells Git that the following argument is a file path, not a branch name.
  • config.yaml is the file you want to copy.

After running this command, config.yaml in your working directory will reflect the version from dev. You can verify with:

git diff HEAD config.yaml   # shows changes between current branch and the checked‑out version

4. Alternative: Using git restore (Git ≥ 2.23)

Newer Git versions provide git restore for checkout‑like operations, which can be clearer about intent:

git restore --source=dev -- config.yaml
  • --source=dev specifies the source branch.
  • -- separates the option from the file path.

Both commands update the working tree and stage the change. If you prefer to keep the change unstaged, add --worktree (or simply omit staging).

5. Keep the Change Staged (Optional)

If you want the file change to be ready for a commit without manually adding it, you can use --stage:

git checkout dev -S -- config.yaml   # -S = --stage

or

git restore --source=dev --stage -- config.yaml

Now the file appears as staged in git status.

6. Verify the Result

Run the following to see the final state:

git status
git diff --cached config.yaml   # shows staged changes (if any)
git diff config.yaml            # shows working‑tree changes (if any)

If everything looks correct, you can commit the file (if needed) using:

git commit -m "Update config.yaml from dev branch"

Why Checkout a Single File

There are several practical scenarios where you might want to git checkout one file from another branch:

  • Borrowing a fix: A hot‑fix is already merged into a release branch, but you need it now in development.
  • Copying configuration: Settings or environment files that are maintained in a separate branch for security.
  • Resolving conflicts: Temporarily replace a file with a version from another branch to see if a conflict can be avoided.
  • Testing: Quickly test a piece of code without switching the entire branch context.

By limiting the operation to a single file, you avoid unnecessary merge commits and keep your branch history clean.

Advanced Techniques

Using git merge with --ours or --theirs

If you need to bring a file from another

branch during an in-progress merge, Git offers conflict-resolution shortcuts. When a merge stops with conflicts, you can choose which side’s version of a file to keep:

# Keep your current branch's version
git checkout --ours config.yaml

# Take the incoming branch's version
git checkout --theirs config.yaml

After making your choice, run git add config.yaml to mark the conflict as resolved, then complete the merge.

Note that git merge -X ours or git merge -X theirs applies the strategy to the entire merge, not

Just Published

Latest Batch

More in This Space

A Few More for You

Thank you for reading about Git Checkout One File From Another Branch. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home