91

OK. So I thought I had this licked ... but now ....

I have a project which includes one small library from GitHub as a submodule. In the original version of that super-project the submodule is working as expected.

However, I just cloned the superproject, did what I thought I should : "git submodule init", got the directory of the submodule to appear, but it's empty.

If I now try to do

git submodule update

I get

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

If I try

git submodule foreach git pull

I get

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

In my .git/config, I have this :

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

In my .gitmodules I have this :

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

Anyone got an idea what's missing?

interstar
  • 26,048
  • 36
  • 112
  • 180

7 Answers7

196

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.

Robert
  • 181
  • 7
Han He
  • 3,391
  • 3
  • 24
  • 25
  • 1
    for me it was a hanged git-fetch that was keeping the folder locked – Mihai Timar Jun 06 '12 at 14:07
  • 35
    I had to remove both the submodule worktree (`ext/blah`) and the matching folder below the `GIT_DIR` (`.git/modules/ext/blah`). – Tobu Jul 13 '12 at 13:06
  • 1
    This looks like a sad thing to do. Fortunately the devs are aware of the problem and hopeful it will be fixed: http://lists-archives.com/git/785138-git-submodule-update-is-not-fail-safe.html – tokland May 22 '13 at 09:03
  • solved for me too. My problem was the there weren't enough permissions to lock the config file, and it stopped too late, fetching an empty library. – krakover Feb 24 '15 at 00:18
  • The failure to clone the submodule caused the clone of the top-level repo to not complete, i.e. I had the README.md but not a subdirectory that was part of the repo. After I re-updated the submodule, I then had to run `git reset --hard` in the top level to get everything. – Alastair Irvine Jul 29 '16 at 09:41
  • That link is broken - @tokland's link in the comments seems relatively informative - that is an unsuccessful `git submodule update` junks the directory and must be removed before trying again. – T. Kiley Oct 03 '16 at 15:39
  • 3
    In my particular case `git submodule update` kept failing on my CI server because it wanted somebody to accept the RSA keys (ci server normally uses https connection and this was the first time a submodule had pulled in ssh connection). Hope it saves somebody an hour of head scratching! – Maverik Jan 02 '17 at 16:34
  • 8
    In 2016 this bug still goes on :( – Paulo Neves Feb 16 '17 at 06:41
  • 7
    2017 checking in, still present. – william.taylor.09 Aug 09 '17 at 14:52
  • 1
    Found the reason this happens for me, if you use git submodule update --init and get your authentication details wrong – Shardj Mar 14 '19 at 10:50
  • Nope, guys, in 2019 you need to INSTALL FRESH GIT. Incrementing year is not enough. – vehsakul Jul 17 '19 at 18:46
  • 1
    The URL for the mailing list reference is broken. `lists-archives.com` server not found. – Craig McQueen Jan 16 '20 at 23:47
5

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.

Ilya Serbis
  • 21,149
  • 6
  • 87
  • 74
  • you may need to run `git submodule init` before `git submodule update` so that the submodule is re-initialized, then this will work. – Pellet Nov 23 '18 at 02:43
3

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
epatel
  • 45,805
  • 17
  • 110
  • 144
1

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

vehsakul
  • 1,118
  • 1
  • 10
  • 17
1

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
loscil
  • 11
  • 2
0

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.

David Lukac
  • 728
  • 7
  • 20
0

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

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141