15

I have a git repository with an attached working tree that I'm pushing to a bare repo on a remote. The repository includes a submodule.

At the remote end: I check out the repo to a working tree git checkout -f having set GIT-DIR and GIT-WORK-TREE env vars.

In the working tree I now see all the expected files and an empty directory for the submodule ('MySubmodule').

I then do:

git submodule init
git submodule update

This errors with a message like:

working tree '../../workTree/' already exists
Clone of 'git@github.com:user/MySubmodule.git' into submodule path 'MySubmodule' failed

The empty submodule directory has now also 'vanished' from the working tree...

I'm not sure where I'm going wrong with this, basically I just want to check out the submodule files as I would with 'git submodule update'.

basicxman
  • 2,095
  • 14
  • 21
arlogb
  • 151
  • 1
  • 3
  • Did you ever get this solved? If so I'd be happy to know how – shevron Nov 08 '11 at 10:04
  • a bare repo isn't supposed to have a working tree and therefore should not have a working tree for a submodule. [similar info](http://stackoverflow.com/questions/1764380/push-to-non-bare-repository#comment29366144_1764380) – Jake Berger Nov 04 '13 at 01:30
  • I had the same problem when writing a deploy script, it seems that there is no way for initializing submodules in a checked out bare repository. – Amir Ali Akbari Dec 26 '14 at 17:38

2 Answers2

2

It looks like when your running "git submodule update" you can't set the GIT_WORK_TREE... it will try to use this as the working tree for the submodule, not for the super project.

I've had to update my servers post-update script...

/usr/local/bin/git --git-dir="$PROJECT_DIR" --work-tree="$PROJECT_DEMO" checkout -f;

cd "$PROJECT_DEMO";
/usr/local/bin/git --git-dir="$PROJECT_DIR" submodule update --init --recursive;

Notice I didn't set env variables, and that the submodule command did not have the "--work-tree" set... it seems that it needs to work from the cwd.

Craig Francis
  • 1,855
  • 3
  • 22
  • 35
  • 7
    This didn't work. remote: fatal: /usr/libexec/git-core/git-submodule cannot be used without a working tree. – berkus Sep 19 '13 at 13:39
  • @DisgruntledGoat would you be able to post a full answer? – Tarek Adam May 17 '18 at 20:24
  • @DisgruntledGoat never mind, I got it thanks to your short comment. Much appreciated. – Tarek Adam May 17 '18 at 20:27
  • Thanks @Craign This saved me a lot of time. `GIT_DIR=/var/www/src/mysite.git GIT_WORK_TREE=/var/www/html git submodule update --init --recursive` was giving me `fatal: /usr/libexec/git-core/git-submodule cannot be used without a working tree.` error while `git --git-dir=/var/www/src/mysite.git/ --work-tree=/var/www/html/ submodule update --init --recursive` worked. :) – MutantMahesh Mar 10 '21 at 06:23
0

First of all, why do you need to checkout a bare repo? Just clone it somewhere else and work with a normal, non-bare repo.

But if you really need to do it, you can rename the folder to .git, change bare = true to bare = false in .git/config and then do git checkout.

sealion
  • 21
  • 4
  • 3
    I use a bare repo for the reasons described here: http://gitready.com/advanced/2009/02/01/push-to-only-bare-repositories.html All of the bare repo functionality works fine apart from checking out the submodule to the detached working tree. – arlogb Sep 24 '11 at 14:34