Git 子模块。进入超级项目的新克隆

好吧,我以为我已经舔过了... 但是现在..。

我有一个项目,其中包括一个来自 GitHub 的小型库作为子模块。在该超级项目的原始版本中,子模块按预期工作。

然而,我只是克隆了超级项目,做了我认为应该做的事情: “ git submodule init”,得到了子模块的目录,但它是空的。

如果我现在尝试去做

git submodule update

我明白

fatal: Needed a single revision
Unable to find current revision in submodule path 'external_libraries/BEACHhtml'

如果我尝试

git submodule foreach git pull

我明白

Entering 'external_libraries/BEACHhtml'
fatal: Where do you want to fetch from today?
Stopping at 'external_libraries/BEACHhtml'; script returned non-zero status.

在我的. git/config 中,我有以下内容:

[submodule "external_libraries/BEACHhtml"]
url = git@github.com:interstar/BEACHhtml.git

在我的. gitmodule 中,我有以下内容:

[submodule "external_libraries/BEACHhtml"]
path = external_libraries/BEACHhtml
url = git@github.com:interstar/BEACHhtml.git

有人知道少了什么吗?

66800 次浏览

use a diff tool to compare the original clone that's working and this one. Also, what does git submodule output. Ensure you are pointing to the same branch in each repo before you do.

I'm suspecting that you've switched to a branch or older revision where the submodule was not defined.

hope this helps

It seems that now (in 2019) installing latest GIT client could solve the problem according to comments below. This should be the best solution for now.


I have the same problem as you. This is a bug in git: http://git.661346.n2.nabble.com/BUG-git-submodule-update-is-not-fail-safe-td7574168.html

In short, for your problem, try:

# rm -rf external_libraries/BEACHhtml
# git submodule update

It seems there is something wrong with the previous checkout folder, remove it, and update again solves the problem.

I had the same issue with a submodule on a project. When I tried to clone the submodule separately it worked well.

I've tried all of the answers above, but without success (git submodule update, ..., removing the submodule folders, ...).

The issue disappeared after update of git (from Git-1.7.11-preview20120710) to latest version (to Git-1.8.1.2-preview20130201) at the time. Strangely my colleagues had even older version, worked without any issues, but they were on Mac. I'm on Win7 64bit.

I had this problem (flaky network so I got dropped submodule checkout like this) and I solved it by making this script (named it git-submodule-fix so I could run it as git submodule-fix)

#!/bin/bash


for arg
do
echo $arg
find . -name "`basename $arg`" | grep "$arg\$" | xargs rm -fr
done

If you get this i.e. from a git submodule update

fatal: Needed a single revision
Unable to find current revision in submodule path 'some/submodule/path'

do

git submodule-fix some/submodule/path
git submodule update

Solved by deleting 2 directories and refetching submodule:

  1. Go to external_libraries/BEACHhtml and look into .git file. It's content should be something like gitdir: ../../.git/modules/external_libraries/BEACHhtml
  2. Delete both external_libraries/BEACHhtml and .git/modules/external_libraries/BEACHhtml directories.

From now on git submodule update runs without errors.

If you are reading in 2019 or later, just update the git client. Worked for me.

I ran into this in 2021 when I got into a detached HEAD state between commits from the sub-module repo's source and it's version in the 'calling' main repo.

this answer helped --> How do I fix a Git detached head?

And I verified this strategy with new changes to the sub-module repo

  1. $ cd <into the sub-module dir within the calling/main repo>
  2. $ git pull --> the sub-module repo brings in any changes from it's source
  3. $ cd <back into the calling/main repo>
  4. $ git status --> should show new changes to be committed for the sub-module repo