Git Checkout File from Another Branch
To get a single file from another branch into your current branch, use git checkout other-branch -- path/to/file. This copies the file as it exists in that branch into your working directory and stages it, without switching branches.
Understanding the Command
Git’s checkout command is versatile. When you specify a branch name followed by -- and a file path, Git interprets it as: “take the file from that branch and place it here.� The -- separator is important because it tells Git that what follows is a file path, not another branch name or option. Without it, Git might misinterpret your intent if a file and branch share a name.
How It Works
When you run git checkout other-branch -- file.txt, Git looks up the version of file.txt in the commit that other-branch points to. It then writes that content into your working directory and adds it to the index (staging area). The result is that the file in your current branch is now updated to match the other branch, and the change is staged ready for commit. Your current branch remains checked out—you stay on the same branch, and no other files are affected.
Practical Examples
Here are common scenarios where you’d use this command:
- Bringing a config file from a feature branch – If a colleague added a useful setting on
feature/configand you want it onmainwithout merging the whole branch. - Restoring a deleted file – If you accidentally deleted a file and it still exists in a previous commit or another branch, you can recover it.
- Comparing or testing a specific file version – You can temporarily bring in an older version to test behavior, then decide whether to keep it.
Choosing the Right Source
You can use any commit reference as the source, not just a branch name. For example:
git checkout main -- README.md– from a branchgit checkout HEAD~2 -- src/app.js– from a commit two steps backgit checkout 4f2a1b9 -- index.html– from a specific commit hash
Using a commit hash is useful when you want the file as it was at a particular point in history, not necessarily at the tip of a branch.
Common Problems and Solutions
Several issues can arise:
- “error: pathspec ‘file’ did not match any file(s) known to git� – The file doesn’t exist in that branch or commit. Double-check the path and the source branch.
- Uncommitted local changes – If you have uncommitted changes to the same file, Git will overwrite them without warning. Use
git stashfirst if you want to keep them. - Merging vs. copying – This command does a full overwrite; it does not merge differences. If you need to combine changes, use
git mergeorgit cherry-pickinstead.
Alternatives to Checkout
Depending on your goal, other commands might be more appropriate:
git restore --source=other-branch -- file.txt– A newer, more explicit command that does the same thing but doesn’t stage the file by default (add--stagedto stage it).git show other-branch:file.txt– Displays the file content without modifying your working directory. Useful for quick inspection.git merge other-branch -- file.txt– Not a real command; merging a single file isn’t directly supported. You’d need to usegit checkoutorgit restoreand then manually merge.
Safety and Best Practices
Because git checkout other-branch -- file overwrites your local file, follow these precautions:
- Check for uncommitted changes with
git statusbefore running the command. - If you have local modifications you want to keep, stash them first:
git stash. - After copying, review the file with
git diff --cachedto see exactly what changed. - Commit the change with a clear message, e.g.,
git commit -m "Update config from feature branch".
Frequently Asked Questions
Does this switch branches?
No. The command only copies the file into your current working directory and stages it. You remain on the same branch.
What if the file doesn’t exist in the other branch?
Git will return an error: “pathspec ‘file’ did not match any file(s) known to git.� Verify the path and branch name, and check if the file is tracked in that branch using git ls-tree other-branch -- path/to/file.
Can I copy multiple files at once?
Yes, you can list multiple paths after the source: git checkout other-branch -- file1.txt file2.txt. You can also copy an entire directory by specifying the folder path.
How is this different from git merge?
git merge integrates entire branches, combining all changes and creating a merge commit. git checkout file only replaces specific files with the exact version from another branch, ignoring all other changes.
Will this work with remote branches?
Yes, but you need to reference the remote-tracking branch, e.g., git checkout origin/main -- file.txt. Ensure you have fetched the latest with git fetch first.
What if I want to unstage the file after copying?
Use git reset HEAD file.txt to unstage it, leaving the file content in your working directory but not staged for commit.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.