Get "please make sure that the .gitmodules file is in the working tree" when running the git submodule add command
Asked Answered
B

5

28

I'm new to git, currently looking at porting some large projects from mercurial. We have a root project that just contains the references to all the external projects (submodules in git). I'm trying to recreate this in git.

I have imported a project (foo) into githib. I've created a new empty project (root) and cloned it locally. I want to add Foo as a submodule using

git submodule add https://github.com/.../foo.git

from the /c/Work/GitHub/root (master)

but I keep getting "please make sure that the .gitmodules file is in the working tree".

Looking at the documentation, the first run of this command should create the .gitmodules file, but I get this error even if I create it by hand. Looking for this error on Google just returns the source files with the error but no explanation to why I'm getting it. I assume it's just my poor understand of git.

What I'm I doing wrong?

EDIT: I've also tried.

mkdir test
cd test
git init
git submodule add https://github.com/.../foo.git

I get the same error.

Beatriz answered 26/5, 2020 at 9:17 Comment(2)
Please provide a full example with exact commands. If necessary, pick a public test repo to treat as a submodule.Topflight
OK found it, looks like a corrupt install of git. I've removed it completely and reinstalled it and the issue has gone away. Thanks for look at this with me.Beatriz
B
-3

Just in case anyone else has this issue. There was something wrong with my git installation. I uninstalled and reinstalled it the error went away.

Beatriz answered 26/5, 2020 at 14:26 Comment(0)
T
22

The check you're probably failing is:

int is_writing_gitmodules_ok(void)
{
        struct object_id oid;
        return file_exists(GITMODULES_FILE) ||
                (get_oid(GITMODULES_INDEX, &oid) < 0 && get_oid(GITMODULES_HEAD, &oid) < 0);
}

This means that either the file exists locally, or it doesn't exist in the staging area or the current HEAD commit.

You've used git add, but then deleted it from the working directory.

Use git restore .gitmodules (or similar) to bring the existing file back into your working directory.

Topflight answered 26/5, 2020 at 9:58 Comment(4)
Cheers for the response, I've not run a git add on this repo yet. I run the command anyway but got 'error: pathspec '.gitmodules' did not match any file(s) known to git 'Beatriz
git restore -s HEAD .gitmodules would be the command to retrieve it from the latest commit.Topflight
same error, my repo only has one commit from when I created it on github. It only has an ignore file. I've cloned it locally and running the submodule command as the first operation on it.Beatriz
If it helps I've just created a new repo, created a test folder, run git init and then the submodule command, get the same error.Beatriz
R
3

You likely removed your .gitmodule or have changes to .gitmodule staged.

Try restoring the file using git restore .gitmodule.

If that doesn't help, try checking git status to ensure nothing is staged; otherwise run git reset .gitmodule.

Romilda answered 16/10, 2020 at 21:51 Comment(0)
J
2

Hard reset e.g. to the last commit or committing all the changes solved the problem for me.

I think the message should be in this case something like "commit your changes first" or so.

Jessiajessica answered 24/7, 2020 at 16:52 Comment(1)
This was the problem for me. Thanks!Sonnnie
K
0

There was something wrong with my git installation. I uninstalled and reinstalled it the error went away.

Considering the current version of git-submodule.sh fails on:

 if ! git submodule--helper config --check-writeable >/dev/null 2>&1

If it possible you had a process keeping an handle on your .gitmodules, preventing any "git submodule add" command to modify it.

Note that this will slightly change With Git 2.33 (Q3 2021), with the rewrite of "git submodule"(man) in C, which does impact git submodule add:

See commit bbe3165 (23 Jul 2021) by Jeff King (peff).
See commit 8c8195e, commit a98b02c, commit 0008d12 (10 Jul 2021), and commit 84069fc (06 Jul 2021) by Atharva Raykar (tfidfwastaken).
(Merged by Junio C Hamano -- gitster -- in commit 10f57e0, 04 Aug 2021)

submodule: prefix die messages with 'fatal'

Signed-off-by: Atharva Raykar
Mentored-by: Christian Couder
Mentored-by: Shourya Shukla

The standard die() function that is used in C code prefixes all the messages passed to it with 'fatal: '.
This does not happen with the die used in 'git-submodule.sh'.

Let's prefix each of the shell die messages with 'fatal: ' so that when they are converted to C code, the error messages stay the same as before the conversion.

Note that the shell version of die exits with error code 1, while the C version exits with error code 128. In practice, this does not change any behaviour, as no functionality in 'submodule add' and 'submodule update' relies on the value of the exit code.

And:

submodule--helper: introduce add-clone subcommand

Signed-off-by: Atharva Raykar
Mentored-by: Christian Couder
Mentored-by: Shourya Shukla
Based-on-patch-by: Shourya Shukla
Based-on-patch-by: Prathamesh Chavan
Helped-by: Đoàn Trần Công Danh

Let's add a new "add-clone" subcommand to git submodule--helper with the goal of converting part of the shell code in git-submodule.sh related to git submodule add(man) into C code.
This new subcommand clones the repository that is to be added, and checks out to the appropriate branch.

This is meant to be a faithful conversion that leaves the behavior of 'cmd_add()' script unchanged.

Karolyn answered 8/8, 2021 at 11:22 Comment(0)
B
-3

Just in case anyone else has this issue. There was something wrong with my git installation. I uninstalled and reinstalled it the error went away.

Beatriz answered 26/5, 2020 at 14:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.