扩展至@The2Step's answer。 (请选择that作为接受的答案)
众所周知,git 是一个分布式源代码控制 repo,您始终拥有该 repo 历史的完整本地副本,并且您需要使用 pull 和 push 操作。
当您在 Azure Devops 或 GitHub 页面中查看内容时,您正在直接查看 最新 远程 strong> 版本的代码。
模拟
运行以下代码将模拟 OP 的状态:
# run this if in PowerShell on windows, it will add a simple touch for
# the code below to work
# function touch([string]$Path){New-Item -ItemType File -Path $Path -Value ''}
mkdir git-playground; cd git-playground
mkdir origin; cd origin
git init -b master
touch first-file.txt
git add first-file.txt
git commit -m="first file committed"
cd ..; git clone origin local; cd origin
touch new-file.txt
git add new-file.txt
git commit -a -m="new file committed"
cd ../local
复制
此时您将位于 local 目录中,指向 master 分支,但使用的是旧版本的 repo。
因此,运行以下命令:
git checkout origin/master new-file.txt
会产生以下错误:
错误:pathspec 'new-file.txt' 与 git 已知的任何文件都不匹配
我猜 OP 目前不想 pull,只想从最新的 origin/master 分支中挑选一个文件,所以第一步是用来自origin 的必要信息。这是通过运行来完成的:
git fetch
这个示例场景的输出对我来说是:
git fetch remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 229 bytes | 5.00 KiB/s, done.
From C:/_/Code/git-playground/origin
bb77593..26b1794 master -> origin/master
并再次运行cherry-pick checkout:
git checkout origin/master new-file.txt
现在应该可以解决了:
Updated 1 path from 550c46d
更多细节
检查状态
git status
将显示我们落后(因为我们从未pulled),并将我们签出的新文件显示为分支的更改:
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: new-file.txt
提示:fetch 是非破坏性的
这是在 repo 的分布式副本之间建立连接的步骤。在操作之前始终使用 remote。