简体   繁体   English

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

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

I am trying to checkout a specific file from my remote master because when I previously pulled in all the new changes from master(via rebase), it deleted certain files from my local repo.我正在尝试从我的远程 master 检出一个特定文件,因为当我之前从 master(通过 rebase)提取所有新更改时,它从我的本地 repo 中删除了某些文件。 I figured that for some reason those files were missing from the master, except they aren't and I see them clear as day in the remote repo.我认为由于某种原因,这些文件从主服务器中丢失了,除了它们不是,而且我在远程仓库中看到它们一清二楚。 Why does git not recognize those files exist?为什么git不识别那些文件的存在?

The command:命令:

git checkout origin/master FormFieldHistoryViewModel.cs

The error:错误:

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

I can see the file sitting there in the repo on Azure Devops.我可以在 Azure Devops 的仓库中看到该文件。 What is going on?到底是怎么回事?

EDIT: sorry, should've mentioned I tried fetching and pulling before posting this.编辑:抱歉,应该提到我在发布之前尝试获取和拉动。

I am thinking you are seeing those files, but you don't have a local copy of them.我认为您正在查看这些文件,但您没有它们的本地副本。 You could try using git fetch before your git checkout command and see if that solves it.您可以尝试在git checkout命令之前使用git fetch ,看看是否可以解决问题。

Expanding on @The2Step's answer .扩展@The2Step 的答案 (please select that as the accepted answer) (请select作为接受的答案)

As we all know git is a distributed source control repo, you always have a complete local copy of the repo's history, and you need to operate with pull and push operations.众所周知, git是一个分布式源代码控制 repo,你总是拥有 repo 历史的完整本地副本,并且你需要使用pullpush操作。

When you're viewing something in Azure Devops or a GitHub page, you are directly viewing the latest remote version of the code.当您查看Azure DevopsGitHub页面中的内容时,您正在直接查看代码的最新远程版本。

Simulation模拟

Running the following code will simulate OP's state:运行以下代码将模拟 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

Reproduction再生产

At this point you will be in the local directory, pointing to the master branch but with an older version of the repo.此时,您将位于本地目录中,指向master分支,但使用的是旧版本的 repo。

Thus, running the following command:因此,运行以下命令:

git checkout origin/master new-file.txt

will producer the following error:将产生以下错误:

error: pathspec 'new-file.txt' did not match any file(s) known to git错误:pathspec 'new-file.txt' 与 git 已知的任何文件都不匹配

@The2Step's Solution @The2Step 的解决方案

I'm guessing OP doesn't want to pull at this time, and just wants to cherry-pick a file from the latest origin/master branch, and so you first step is to update the local repo with the necessary information from origin .我猜 OP 此时不想pull ,只想从最新的origin/master分支中挑选一个文件,所以你的第一步是用来自origin的必要信息更新本地仓库。 That's done by running:这是通过运行完成的:

git fetch

output of this example scenario for me was:对我来说,这个示例场景的 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

and run the cherry-pick checkout once more:并再次运行 cherry-pick checkout:

git checkout origin/master new-file.txt

which should now work out:现在应该可以解决:

Updated 1 path from 550c46d

Some more details更多细节

Checking status检查状态

git status

will show that we are behind (because we never pull ed), and will show the new file we checked out as a change of the branch:将显示我们落后了(因为我们从未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

TIP: fetch is non-destructive提示: fetch是非破坏性的

It is the step that makes the connection between the distributed copies of the repo.这是在 repo 的分布式副本之间建立连接的步骤。 Always use it before operations with a remote .在使用remote操作之前始终使用它。

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

相关问题 为什么我在 iOS(颤振应用程序)中收到 Firebase AppCheck 错误 - Why am I getting a Firebase AppCheck error in iOS (Flutter app) 为什么我在使用 firebase 时收到尝试导入错误? - Why am I getting the attempted import error when using firebase? 在这种情况下,为什么我会收到 Bad state 错误? - Why am I getting a Bad state error in this case? 为什么我在使用 Compute Engine 时遇到错误 - Why am I getting an error using Compute Engine 为什么在 Google BigQuery 上安排查询时出现错误? - Why am I getting an error when scheduling a query on Google BigQuery? 为什么我在此代码中收到类型不匹配错误? - Why am I getting a type mismatch error in this code? 为什么在运行以下 SQL 代码时出现错误 - Why am I getting an error while running the following SQL code 在创建 model 时,我在 BigQuery 中收到“CREATE MODEL OPTIONS() 格式参数丢失”错误? - I am getting "CREATE MODEL OPTIONS() format parameter is missing" error in BigQuery while creating a model? 通过 git 存储库将我的应用程序部署到 azure 时出现错误 - I am getting an error while deploying my app to azure through git repository firebase.database 不是 function 为什么我在反应本机应用程序中收到此错误? - firebase.database is not a function why I am getting this error in react native app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM