8

I have a git repo with a submodule.

I would use in post-receive hook file:

git --git-dir="$GIT_DIR" --work-tree="$GIT_WORKDIR1" submodule update --init --recursive 

but I get the following error:

remote: fatal: /usr/libexec/git-core/git-submodule cannot be used without a working tree.  

I did not get solution for this problem.

What should I do to make it work?

klor
  • 1,237
  • 4
  • 12
  • 37

4 Answers4

4

You may see this error if you've renamed the path (working-tree) of a git submodule. In my case I had updated the path in .gitmodules to match my new path and thought I was good. But when I did a git pull later it added new files in the old path. This is because there are two places the module path is defined. You need to also update your "working-tree" as defined in the .git/modules/{modulename}/config file.

There are some great details about the working-tree on this post as well.

Code Commander
  • 16,771
  • 8
  • 64
  • 65
  • Thanks for your answer. I work on another task currently, will check this problem, when I return to this task. – klor Mar 29 '18 at 11:38
1

I got this same error when running git submodule inside of a shell I spawned while I was running git log (with the !bash command). What also confused me was that I could run all git commands if I ran them with sudo, and that this problem affected multiple repos, not only the one I was running git log into.

The solution of course is to exit the subshell and the git log command.

AlejandroVD
  • 1,576
  • 19
  • 22
0

I also stumbled across this issue and found it is caused, because I needed to cd into the workdir first. I am not sure why this is required, maybe it is a git bug? You can also pass the path via -C and set the workdir to .:

git --git-dir="$GIT_DIR" --work-tree=. -C "$GIT_WORKDIR1" submodule update --init --recursive

Also I noticed that in my plesk git user interface (netcup.com) variables do not seems to work, I have to specify absolute paths without variables here:

git --git-dir=/git/repo_name.git --work-tree=. -C /httpdocs/website submodule update --init --recursive
NicoHood
  • 687
  • 5
  • 12
0

FYI for future readers: this also happens on Windows if you cd to your repo folder via a symbolic link, as the working directory reported by the OS doesn't match the truth.

Ammo Goettsch
  • 838
  • 8
  • 11