无法摆脱 Git 子模块

14

我在我的Rails 3项目中添加了一些Haml模板:

git clone git://github.com/psynix/rails3_haml_scaffold_generator.git lib/generators/haml

后来我发现,当我尝试编辑其中一些文件时,这是一个子模块,因此我无法提交我在lib/generators/haml目录中所做的更改。现在每次我使用git status命令都会得到以下提示:

# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   lib/generators/haml
#
no changes added to commit (use "git add" and/or "git commit -a")

但是git add lib/generators/haml没有作用。我只想要文件,而不是子模块,但是我发现无法摆脱子模块:

> git rm --cached lib/generators/haml
rm 'lib/generators/haml'
> git status

# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    lib/generators/haml
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   lib/generators/

> git commit -m "Removed submodule"

[master 02ae4c7] Removed submodule
 1 files changed, 0 insertions(+), 1 deletions(-)
 delete mode 160000 lib/generators/haml

> git status

# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   lib/generators/
nothing added to commit but untracked files present (use "git add" to track)

> git add lib/generators/haml
> git status

# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   lib/generators/haml
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   lib/generators/haml

> git commit -m "Re-added lib/generators/haml"

[master c966912] Re-added lib/generators/haml
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 160000 lib/generators/haml

> git status

# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   lib/generators/haml

多余的git add是无用的。如果我尝试使用git rm -rf lib/generators/haml,我会得到以下结果:

fatal: git rm: 'lib/generators/haml': Operation not permitted

它就是死不掉!我查阅了如何删除子模块,但在这种情况下,没有.submodules文件,也没有在.git/config中提及子模块。但是,如果我运行git submodule update,我会得到以下信息。

No submodule mapping found in .gitmodules for path 'lib/generators/haml'

我已经删除了这个目录,但是结果仍然一样!发生了什么?我需要创建一个.gitmodules文件才能删除子模块吗?

3个回答

6

如果子模块有未跟踪的文件,它将在git状态中显示为已修改。如果您调用git diff lib/generators/haml,您很可能会看到类似于以下内容:

diff --git a/lib/generators/haml b/lib/generators/haml
index 3019fec..653c59a 160000
--- a/lib/generators/haml
+++ b/lib/generators/haml
@@ -1 +1 @@
-Subproject commit 653c59ad72925c9ccbde67e8e484e15d4b6dd25d
+Subproject commit 653c59ad72925c9ccbde67e8e484e15d4b6dd25d-dirty

这意味着在这个子模块中有一些未被跟踪的文件。它们不能通过父项目中的命令添加;相反,您应该进入子模块并添加它们(或添加到.gitignore条目)... 或者您可以忽略状态消息。
最新的Git版本(我想是1.7.1)在状态中显示此信息:
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   lib/generators/haml (untracked content)

好的,我想我明白了:每当我更改lib/generators/haml中的内容时,我应该进入该目录,然后进行git操作。正如你建议的那样,我将我的git从1.7.0.5更新到了1.7.2,这确实给了我更有用的消息。仍然让我感到困惑的是,即使我删除了lib/generators/haml及其.git目录,git仍然认为存在一个子模块。 - Trevor Burnham

6
如果没有子模块,那么它(即 'haml')实际上可能不是子模块吗?
git clone git://github.com/psynix/rails3_haml_scaffold_generator.git lib/generators/haml

意思是:创建lib/generators/haml目录,并在其中检出git://github.com/psynix/rails3_haml_scaffold_generator.git的主分支。
事实上,您可以从您的新存储库的根目录(其中包含lib)而不是三个级别以上进行git status,这意味着您在存储库中克隆了一个存储库。
两个存储库都可以独立工作,但您应将lib/generators/haml添加到父存储库的.gitignore中(如Stack Overflow问题所述)。

1
但是这不会将新文件推送到仓库,供您参考。 - andho
我的问题最终与此无关,但这确实有所帮助。 - New Alexandria

0

你本可以使用以下代码:

git add lib/generators/haml/

将子模块中的文件添加到HEAD并删除子模块。但是,如果您实际上没有子模块,则可能无法正常工作。

请注意路径后面有一个斜杠。如果您这样做,git会删除子模块并将其放入HEAD,而不是:

git add lib/generators/haml

这将仅更改在 Git 子模块中要检出的提交。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接