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.
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.
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.
Older but Still Common Command: git checkout
Older Git examples use git checkout for the same job.
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:
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.
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.
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.
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 leavesorigin/masteroutdated. - 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 statusfirst. - Using
origin/masterin repositories that actually useorigin/main. - Assuming the file is committed automatically after restore; it still needs
git addandgit commitif you want to keep the change.
Summary
- Fetch the remote first so your local
origin/masterreference is current. - Use
git restore --source origin/master -- path/to/filein modern Git. - '
git checkout origin/master -- path/to/fileis 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.

