首页 > 解决方案 > git restore 一个实际未更改的文件夹

问题描述

我有一个修改过的文件夹(但是,我想不出任何机会)并且想要取消暂存(并保留)其内容,因为它是这里的子模块,里面有代码。

C:\Users\devuser\source\repos\MainProject>git status
On branch master
Your branch is up to date with 'origin/master'.

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:   SubProject (new commits)

no changes added to commit (use "git add" and/or "git commit -a")

到目前为止,我知道我可以使用git restore SubProject. 如果我说git restore SubProject我静下来

C:\Users\devuser\source\repos\MainProject>git status
On branch master
Your branch is up to date with 'origin/master'.

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:   SubProject (new commits)

no changes added to commit (use "git add" and/or "git commit -a")

经过一段时间的研究,我遇到了这个问题。我究竟做错了什么?

标签: gitgit-submodules

解决方案


编辑:第一个解决方案只是暂时的。我在这个 repo 上再次遇到了同样的问题。

永久解决方案:我发现这SubProject 是一个以前的 git repo,后来被集成到 MainProject 中。

SubProject 有.git自己的文件夹,因为每次编译整个项目时它总是抱怨未跟踪的更改。

所以我删除.git了 SubProject 中的文件夹,并通过git status.

我保留了临时解决方案(可能对另一个解决方案有帮助)。

临时解决方案: 解决方案是将文件夹添加到主项目。

C:\Users\devuser\source\repos\MainProject>git add SubProject

C:\Users\devuser\source\repos\MainProject>git add SubProject/

C:\Users\devuser\source\repos\MainProject>git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   SubProject

然后我将其提交到 repo。

C:\Users\devuser\source\repos\MainProject>git commit -m "SubProject was unstaged?!"
[master 1042efc] SubProject was unstaged?!
 1 file changed, 1 insertion(+), 1 deletion(-)

C:\Users\devuser\source\repos\MainProject>git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 266 bytes | 266.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Analyzing objects... (2/2) (196 ms)
remote: Storing packfile... done (54 ms)
remote: Storing index... done (48 ms)
To https://my.repo.com/DefaultCollection/MainProject/_git/MainProject
   b901854..1042efc  master -> master

再次检查git status我得到了

On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

摘要:我假设我只添加了子项目,但没有与 MainProject 一起添加。


推荐阅读