【问题标题】:Why am I getting an error that file is missing on git checkout?为什么我在 git checkout 上收到文件丢失的错误?
【发布时间】:2021-08-23 16:38:18
【问题描述】:

我正在尝试从远程 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 的存储库中看到该文件。怎么回事?

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

【问题讨论】:

  • 这可能不需要 [C#] 标签
  • 我在这里猜测您实际上并没有在正确的时间运行git fetch,但另一种可能性是您可能需要git checkout origin/master -- FormFieldHistoryViewModel.cs。我用最近的 Git 对此进行了测试,git checkout 不需要明确的--,但在某些情况下您偶尔会需要一个;也许旧版本的 Git 有更多。

标签: git azure visual-studio azure-devops


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    扩展至@The2Step's answer。 (请选择that作为接受的答案)

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

    当您在 Azure DevopsGitHub 页面中查看内容时,您正在直接查看 最新 远程 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 已知的任何文件都不匹配

    @The2Step's Solution

    我猜 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

    【讨论】:

    • 我先取了。仍然没有工作。起作用的是在 vs 解决方案资源管理器中点击“显示所有文件”。我在虚线轮廓中看到了丢失的文件。我选择了“包含在项目中”并且它起作用了。你知道我为什么一开始就必须这样做吗?
    • 您所描述的内容听起来很奇怪,特定于 Azure Devops,我的回答来自 git 管理方面。希望你能找到答案。
    猜你喜欢
    • 2015-08-19
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 2021-12-26
    相关资源
    最近更新 更多