Magpie
Magpie

Reputation: 7173

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

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.

Upvotes: 29

Views: 31764

Answers (6)

Shital Shah
Shital Shah

Reputation: 68728

For anyone who is getting this mysterious error in Git version 2.34.1:

This error happens if you have multiple submodules in your repo, you remove one and then try to add another one but tried to specify the branch for the submodule with -b switch like this:

git submodule add -b stable https://github.com/sytelus/congo.git themes/congo

The error doesn't make sense at all because .gitmodules clearly exist in working directory and nothing makes this error go away except just not to use -b switch in git submodule add. I believe this is bug in git.

To workaround this, add submodule without specifying branch and then manually add branch in .gitmodules like this:

[submodule "themes/congo"]
    path = themes/congo
    url = https://github.com/sytelus/congo.git
    branch = stable

Then cd into your submodule folder and change branch using git checkout.

Upvotes: 0

VonC
VonC

Reputation: 1324278

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.

Upvotes: 0

Trent
Trent

Reputation: 4306

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.

Upvotes: 4

Juri Sinitson
Juri Sinitson

Reputation: 1635

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.

Upvotes: 2

Magpie
Magpie

Reputation: 7173

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.

Upvotes: -3

Joe
Joe

Reputation: 31087

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.

Upvotes: 22

Related Questions