请参阅我原来的问题的实线下方。
我的本地目录中有一个未跟踪的文件夹。当我跑 git status
,我得到:
Changed but not updated:
modified: vendor/plugins/open_flash_chart_2 (modified content, untracked content)
当我输入
git add vendor/plugins/open_flash_chart_2
然后尝试 git status
再次,它仍然说未跟踪。这是怎么回事?这是我最近半小时的简单总结:
vendor/plugins/open_flash_chart_2
插入。具体来说,没有内容,它在文件夹图标上显示一个绿色箭头。 git submodule init
No submodule mapping found in .gitmodules for path 'vendor/plugins/open_flash_chart_2'
git submodule add git://github.com/korin/open_flash_chart_2_plugin.git vendor/plugins/open_flash_chart_2
vendor/plugins/open_flash_chart_2 already exists in the index
git status
modified: vendor/plugins/open_flash_chart_2 (untracked content)
.gitmodules
的文件在我的存储库/本地目录中,但找不到。 我该怎么做才能让我的子模块正常工作,这样 git 才能开始正确跟踪?
这可能是无关的(我把它包括在内,以防它有帮助),但每次我输入
git commit -a
而不是我通常的 git commit -m "my comments"
,它抛出一个错误:E325: ATTENTION
Found a swap file by the name ".git\.COMMIT-EDITMSG.swp"
dated: Thu Nov 11 19:45:05 2010
file name: c:/san/project/.git/COMMIT_EDITMSG
modified: YES
user name: San host name: San-PC
process ID: 4268
While opening file ".git\COMMIT_EDITMSG"
dated: Thu Nov 11 20:56:09 2010
NEWER than swap file!
Swap file ".git\.COMMIT_EDITMSG.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:
Swap file ".git\.COMMIT_EDITMSG.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:
我是 Github 的完全新手,尽管我试图阅读文档,但我还是被这些特殊问题难住了。谢谢你。
最佳答案
您已添加 vendor/plugins/open_flash_chart_2
作为“gitlink”条目,但从未将其定义为子模块。实际上,您正在使用 git submodule 使用的内部功能(gitlink 条目),但您没有使用子模块功能本身。
你可能做了这样的事情:
git clone git://github.com/korin/open_flash_chart_2_plugin.git vendor/plugins/open_flash_chart_2
git add vendor/plugins/open_flash_chart_2
最后一个命令是问题所在。目录
vendor/plugins/open_flash_chart_2
最初是一个独立的 Git 存储库。通常这样的子存储库会被忽略,但是如果你告诉 git add 显式添加它,那么它会创建一个 gitlink 条目,指向子存储库的 HEAD 提交,而不是添加目录的内容。如果 git add 拒绝创建这样的“半子模块”可能会很好。普通目录在 Git 中表示为树对象;树对象为其包含的对象提供名称和权限(通常是其他树和 blob 对象——分别是目录和文件)。子模块表示为“gitlink”条目; gitlink 条目仅包含子模块的 HEAD 提交的对象名称(哈希)。 gitlink 提交的“源存储库”在
.gitmodules
中指定文件(以及子模块初始化后的 .git/config
文件)。您拥有的是一个指向特定提交的条目,而无需记录该提交的源存储库。您可以通过将 gitlink 放入适当的子模块,或通过删除 gitlink 并将其替换为“正常”内容(纯文件和目录)来解决此问题。
把它变成一个合适的子模块
您缺少正确定义
vendor/plugins/open_flash_chart_2
的唯一一点作为子模块是 .gitmodules
文件。通常(如果您还没有将其添加为 gitlink 裸条目),您只需使用 git submodule add
:git submodule add git://github.com/korin/open_flash_chart_2_plugin.git vendor/plugins/open_flash_chart_2
如您所见,如果该路径已存在于索引中,这将不起作用。解决办法是暂时从索引中删除gitlink条目,然后添加子模块:
git rm --cached vendor/plugins/open_flash_chart_2
git submodule add git://github.com/korin/open_flash_chart_2_plugin.git vendor/plugins/open_flash_chart_2
这将使用您现有的子存储库(即它不会重新克隆源存储库)并暂存
.gitmodules
看起来像这样的文件:[submodule "vendor/plugins/open_flash_chart_2"]
path = vendor/plugins/open_flash_chart_2
url = git://github.com/korin/open_flash_chart_2_plugin.git vendor/plugins/open_flash_chart_2
它还将在您的主存储库的
.git/config
中创建一个类似的条目。 (没有 path
设置)。提交它,您将拥有一个适当的子模块。当您克隆存储库(或推送到 GitHub 并从那里克隆)时,您应该能够通过
git submodule update --init
重新初始化子模块。 .将其替换为纯内容
下一步假设您的子存储库位于
vendor/plugins/open_flash_chart_2
没有您想要保留的任何本地历史记录(即您只关心子存储库的当前工作树,而不是历史记录)。如果您关心的子存储库中有本地历史记录,那么您应该备份子存储库的
.git
在下面的第二个命令中删除它之前的目录。 (还要考虑下面的 git subtree 示例,它保留了子存储库的 HEAD 的历史记录)。git rm --cached vendor/plugins/open_flash_chart_2
rm -rf vendor/plugins/open_flash_chart_2/.git # BACK THIS UP FIRST unless you are sure you have no local changes in it
git add vendor/plugins/open_flash_chart_2
这次添加目录的时候,不是子仓库,所以文件会正常添加。不幸的是,由于我们删除了
.git
目录中没有 super 简单的方法来使源存储库保持最新状态。您可能会考虑使用 subtree merge反而。这样做可以让您轻松地从源存储库中提取更改,同时保持存储库中的文件“扁平”(无子模块)。第三方git subtree command是围绕子树 merge 功能的一个很好的包装器。
git rm --cached vendor/plugins/open_flash_chart_2
git commit -m'converting to subtree; please stand by'
mv vendor/plugins/open_flash_chart_2 ../ofc2.local
git subtree add --prefix=vendor/plugins/open_flash_chart_2 ../ofc2.local HEAD
#rm -rf ../ofc2.local # if HEAD was the only tip with local history
之后:
git remote add ofc2 git://github.com/korin/open_flash_chart_2_plugin.git
git subtree pull --prefix=vendor/plugins/open_flash_chart_2 ofc2 master
git subtree push --prefix=vendor/plugins/open_flash_chart_2 git@github.com:me/my_ofc2_fork.git changes_for_pull_request
git subtree 也有一个
--squash
选项可让您避免将源存储库的历史记录 merge 到您的历史记录中,但仍允许您引入上游更改。
关于git - 如何跟踪未跟踪的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4161022/