[英]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 历史的完整本地副本,并且你需要使用pull
和push
操作。
When you're viewing something in Azure Devops or a GitHub page, you are directly viewing the latest remote version of the code.当您查看Azure Devops或GitHub页面中的内容时,您正在直接查看代码的最新远程版本。
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
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 已知的任何文件都不匹配
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
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
fetch
is non-destructivefetch
是非破坏性的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.