Git: 如何从远程分支update/checkout 单个文件?

Git: How to update/checkout a single file from remote branch?

我正在尝试从远程分支签出单个文件,但在使用正确语法时遇到了问题。如果这个文件在 master 上,我可以 运行 (link):

$ git checkout origin/master -- path/to/file

但是如果我 运行 使用我的分支名称执行类似的命令,我会得到:

$ git checkout origin/my-branch-name -- path/to-file
fatal: invalid reference: origin/my-branch-name

在远程分支上签出此单个文件的等效命令是什么?

在链接的问题中 origin/master 不是 remote 分支 — 它是 local 远程跟踪分支。如果您没有本地分支,您需要先获取它:

git fetch origin
git branch -r # list local remote-tracking branches
git checkout origin/my-branch-name -- path/to-file