繁体   English   中英

为什么我在 git 结帐时收到文件丢失的错误消息?

[英]Why am I getting an error that file is missing on git checkout?

我正在尝试从我的远程 master 检出一个特定文件,因为当我之前从 master(通过 rebase)提取所有新更改时,它从我的本地 repo 中删除了某些文件。 我认为由于某种原因,这些文件从主服务器中丢失了,除了它们不是,而且我在远程仓库中看到它们一清二楚。 为什么git不识别那些文件的存在?

命令:

git checkout origin/master FormFieldHistoryViewModel.cs

错误:

error: pathspec 'FormFieldHistoryViewModel.cs' did not match any file(s) known to git

我可以在 Azure Devops 的仓库中看到该文件。 到底是怎么回事?

编辑:抱歉,应该提到我在发布之前尝试获取和拉动。

我认为您正在查看这些文件,但您没有它们的本地副本。 您可以尝试在git checkout命令之前使用git fetch ,看看是否可以解决问题。

扩展@The2Step 的答案 (请select作为接受的答案)

众所周知, git是一个分布式源代码控制 repo,你总是拥有 repo 历史的完整本地副本,并且你需要使用pullpush操作。

当您查看Azure DevopsGitHub页面中的内容时,您正在直接查看代码的最新远程版本。

模拟

运行以下代码将模拟 OP 的 state:

# 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

再生产

此时,您将位于本地目录中,指向master分支,但使用的是旧版本的 repo。

因此,运行以下命令:

git checkout origin/master new-file.txt

将产生以下错误:

错误:pathspec 'new-file.txt' 与 git 已知的任何文件都不匹配

@The2Step 的解决方案

我猜 OP 此时不想pull ,只想从最新的origin/master分支中挑选一个文件,所以你的第一步是用来自origin的必要信息更新本地仓库。 这是通过运行完成的:

git fetch

对我来说,这个示例场景的 output 是:

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

将显示我们落后了(因为我们从未pull ed),并将显示我们签出的新文件作为分支的更改:

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操作之前始终使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM