GIT error: fatal: /usr/libexec/git-core/git-submodule cannot be used without a working tree
Asked Answered
N

4

10

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?

Nickels answered 13/2, 2018 at 13:9 Comment(0)
N
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.

Nakisha answered 28/3, 2018 at 22:18 Comment(1)
Thanks for your answer. I work on another task currently, will check this problem, when I return to this task.Nickels
C
3

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
Camion answered 31/10, 2020 at 11:6 Comment(2)
This fixed it for me. It certainly appears to be a git submodule bug, where it doesn't respect --work-tree. It's still present as of git version 2.39.2Selfhelp
This also fixes checkout --recurse-submodules: git --git-dir="${GIT_DIR}" --work-tree=. -C "${SOURCE_DIR}" checkout --recurse-submodules "${BRANCH}" -- . With the work-tree set directly, this would checkout the source into the work-tree directory, but silently fail to checkout any submodules.Selfhelp
L
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.

Labrador answered 30/7, 2018 at 22:20 Comment(0)
S
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.

Spade answered 11/2, 2023 at 14:42 Comment(1)
It happened for me on Windows when using the Git Bash, probably for the same reason? (The 'simulated' /d/folder/repo does not match D:\folder\repo)Kaczmarek

© 2022 - 2024 — McMap. All rights reserved.