git
version control
checkout
file update
remote repository

Git How to update/checkout a single file from remote origin master?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you want one file from origin/master, you do not need to reset your whole branch or pull everything into your working tree. The clean workflow is to fetch the remote branch, then restore or check out the specific path from that remote-tracking reference.

Fetch Before You Restore the File

Your local repository needs an up-to-date copy of the remote branch tip. Start by fetching.

bash
git fetch origin

After that, origin/master refers to the latest fetched state of the remote master branch. If the repository uses main instead, substitute origin/main in the examples below.

Modern Command: git restore

In newer Git versions, git restore is the most explicit way to replace one file in your working tree from another commit or branch.

bash
git fetch origin
git restore --source origin/master -- path/to/file.txt

That updates the working-tree copy of path/to/file.txt to match the version stored in origin/master. It does not merge the branch and it does not modify unrelated files.

If you also want to stage the change immediately, add --staged.

bash
git restore --source origin/master --staged --worktree -- path/to/file.txt

Older but Still Common Command: git checkout

Older Git examples use git checkout for the same job.

bash
git fetch origin
git checkout origin/master -- path/to/file.txt

This is still valid and widely seen in documentation and answers, but git restore is usually easier to read because it separates “restore a file” from “switch branches.”

What Actually Happens

You are not checking out the whole remote branch. You are copying the blob for one tracked path from the tree at origin/master into your current branch's working tree. The branch itself does not move. Your HEAD does not change. Only that file changes locally.

That means the normal next steps still apply:

bash
git diff -- path/to/file.txt
git add path/to/file.txt
git commit -m "Update file from origin/master"

Until you commit, the change is just a local modification in your current branch.

If You Only Want to Inspect the Remote Version

Sometimes you do not want to overwrite the local file yet. You can print or save the remote version without touching the working tree.

bash
git show origin/master:path/to/file.txt

That is useful for comparison, debugging, or copying a small snippet manually.

Uncommitted Local Changes Need Care

If the file already has local edits, restoring from the remote branch will replace those working-tree contents. Check first.

bash
git status --short

If the file is modified and you might need the local version later, either commit it, stash it, or copy it aside before restoring from origin/master.

Remote Branch Names May Differ

The title says origin/master, but many repositories now use main. The pattern is identical.

bash
git fetch origin
git restore --source origin/main -- src/config.yml

The important part is to restore from the correct remote-tracking branch, not to assume master exists.

Common Pitfalls

  • Running the restore command before git fetch origin, which leaves origin/master outdated.
  • Confusing “restore one file from a remote branch” with “switch my whole branch to the remote branch.”
  • Overwriting local uncommitted changes in the target file without checking git status first.
  • Using origin/master in repositories that actually use origin/main.
  • Assuming the file is committed automatically after restore; it still needs git add and git commit if you want to keep the change.

Summary

  • Fetch the remote first so your local origin/master reference is current.
  • Use git restore --source origin/master -- path/to/file in modern Git.
  • 'git checkout origin/master -- path/to/file is the older equivalent.'
  • Only the specified file changes; your current branch and HEAD stay where they are.
  • Review, stage, and commit afterward if you want the restored file to become part of your branch history.

Course illustration
Course illustration

All Rights Reserved.