我不想 stage/commit 更改子模块。不幸的是,我无法恢复它。我应该怎么办?

There was a change on a submodule that I do not want to stage/commit. Unfortunately, I cannot restore it. What should I do?

我是 fetching/pulling 一个开源项目的依赖项。我这样做是为了更新内容。有必要 运行 程序。

当我这样做时,主存储库构建文件中发生了一些不需要的更改。 Git 显示发生的变化:

Changes are not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        modified:   _build/submodules/cl-webkit (new commits)

正如我所做的那样git diff,我可以看出这是一个简单的改变:

$ git diff _build/submodules/cl-webkit/
diff --git a/_build/submodules/cl-webkit b/_build/submodules/cl-webkit
index cfc4f01e..29aeb74d 160000
--- a/_build/submodules/cl-webkit
+++ b/_build/submodules/cl-webkit
@@ -1 +1 @@
-Subproject commit cfc4f01ee806169d824750b4014653a93af9353d
+Subproject commit 29aeb74d854bd2ac52959ef3d793389d238636c5

更改保持未暂存和未提交状态。如果我尝试使用以下方法恢复它们:

$ git restore _build/submodules/cl-webkit/

我没有收到错误消息。但是,它 起作用。按 git status 后,git 显示内容保持不变:

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   _build/submodules/cl-webkit (new commits)

我该怎么办?

这很烦人。如果我向开源项目发出合并请求,我总是需要证明我是否不小心添加(暂存)并提交了此更改。它至少发生了两次,维护者要求我恢复提交并在没有它的情况下推送 PR。

更新子模块的正确命令是git submodule update

git submodule update _build/submodules/cl-webkit

(我也很惊讶 git restoregit checkout 都不起作用)


由于子模块也是一个 git 仓库,您可以通过 运行 常规 git 命令从该子模块中更新它:

# if you can afford to delete all changes, you can use 'reset --hard'
cd _build/submodules/cl-webkit
git reset --hard cfc4f01ee806169d824750b4014653a93af9353d