3

When I am trying to add all files to gitRepo, it says:

$ git add .
error: 'ConnectorApp/' does not have a commit checked out
fatal: adding files failed

When I am trying to delete the empty folder, it says:

$ git rm ConnectorApp/
fatal: pathspec 'ConnectorApp/' did not match any files

Is there any other solution to clear this empty folder to add files to my gitRepo?

AD7six
  • 63,116
  • 12
  • 91
  • 123
Hadassa
  • 33
  • 1
  • 4
  • Possible duplicate of [git submodule add error: does not have a commit checked out](https://stackoverflow.com/questions/56799562/git-submodule-add-error-does-not-have-a-commit-checked-out) – AD7six Nov 20 '19 at 17:22
  • 1
    Looks like the folder is not recognised as a gift repository at all. Do you see a .git folder in the root folder? If not, you don't have a clone or checkout. You can use git init to initialize – Denounce'IN Nov 20 '19 at 17:30

1 Answers1

6

It's not an empty folder. It is a submodule.

An empty folder would be irrelevant, because Git stores only files, not folders. A file's path name may be something like dir/file.ext. That's not a folder dir containing file.ext, but rather, just a file whose name is dir/file.ext. If your OS insists on storing such a file as a folder named dir containing a file named file.ext when you need to work on/with it, well, that's OK: Git will create a new empty dir folder and write into it a file named file.ext when necessary, during git checkout for instance. But Git has simply stored the file dir/file.ext. It has not stored dir.

But in this case, if you look more closely at ConnectorApp/, including looking for normally-hidden files and directories/folders, you will see that there is a ConnectorApp/.git. In other words, ConnectorApp/ itself is its own Git repository. A Git repository cannot contain another Git repository,1 so instead, your Git repository here will record that second Git repository as what Git calls, internally, a gitlink.

The mechanism behind a gitlink is that your Git can invoke a second sub-Git on your own machine, to enter the sub-repository—which Git calls a submodule—and ask it: Which commit is this repository using? The answer, if there is an answer, will be a raw commit hash ID.

The outer repository—which Git calls the superproject—then records, in each commit, this raw hash ID. Running git add ConnectorApp or git add ConnectorApp/2 tells your superproject Git to enter the submodule just long enough to find out the right hash ID and store/update it, as a gitlink entry.

The error message tells you that right now, the submodule—the other Git—does not have any commit checked out. So the superproject Git invokes the submodule Git and asks it which commit do you have checked out? and the submodule Git says get the ____ out of here, I don't have any!

To fix the problem, you have several options:

  1. Don't use submodules. Don't try to add ConnectorApp/ at all. Have your superproject be a Git without a submodule, and list ConnectorApp/ in .gitignore in the superproject so that it doesn't try to add it.

    This option only works if the superproject doesn't already list ConnectorApp as a submodule.

  2. Enter the submodule yourself, and pick out a commit. That is:

    cd ConnectorApp/
    git checkout <something>
    cd ..                      # return to superproject
    

    What you fill in for the something here determines which commit hash ID the superproject Git will see when the superproject Git asks the submodule Git which commit hash ID do you have checked out?

    This is how your superproject commit can record the correct commit to be used in the submodule.

  3. If your superproject Git has an existing recorded correct commit, you can have the superproject Git tell the submodule Git to check out the recorded commit:

    git submodule update --checkout
    

    (actually --checkout is the default, so you can typically omit it, but there are configuration items that can change this).

    Note that this applies to every submodule listed in the superproject. If there is only the one submodule, that's OK.

(Note that you can have improperly created submodules, where the superproject has a gitlink without the rest of the information required. This is probably not the case here, but it does happen sometimes. If it has happened, search StackOverflow for answers about that.)


1There's no technical reason that one repository could not contain another one wholesale, but there are administrative reasons not to do that, so Git is programmed not to do that.

2In the bad old days, git add ConnectorApp/ would go ahead and add all the files from the submodule to the superproject. This behavior is fixed now, which makes submodules much more workable than they used to be. If you have an old Git, though, be careful: don't let that trailing slash in there! I still have some burn scars from Git 1.5 or 1.6 days here.

torek
  • 448,244
  • 59
  • 642
  • 775
  • 1
    **This worked for me:** I removed the submodule's folder and removed the submodule declaration in `.gitmodules`. After this, I was able to add, commit and push. Finally, I did: `git submodule add ` again. – Derk Jan Speelman Feb 20 '20 at 14:17