Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 829
Magpie Avatar asked May 26 '20 09:05

Magpie


People also ask

Where is the .gitmodules file?

The . gitmodules file, located in the top-level directory of a Git working tree, is a text file with a syntax matching the requirements of git-config[1]. The file contains one subsection per submodule, and the subsection value is the name of the submodule.

What is git submodule command?

Git submodules allow you to keep a git repository as a subdirectory of another git repository. Git submodules are simply a reference to another repository at a particular snapshot in time. Git submodules enable a Git repository to incorporate and track version history of external code.


3 Answers

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.

like image 126
Joe Avatar answered Sep 19 '22 23:09

Joe


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.

like image 22
Trent Avatar answered Sep 18 '22 23:09

Trent


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.

like image 31
Juri Sinitson Avatar answered Sep 16 '22 23:09

Juri Sinitson