22

I have a cloned repo. There's a directory, call it a/b/c that used to be its own repo, i.e. there was a/b/c/.git/ etc.

Now I want the files managed in the main repo. I don't care about the history in a/b/c, so I deleted the .git dir in a/b/c

But the problem is that git status is ignoring a/b/c completely. I can't git add it. It's as though I'd put the path into .gitignore (I haven't).

Obviously before, it made sense for git to ignore a subdir with a .git dir in it, but now how does it know the difference?

Is there somewhere else that ignore files are listed other than .gitignore and .git/info/excludes? There's nothing in the .git/config file?

I've been asked what git status says. Not much:

/path/to/root/dir $ git status
# On branch fred
nothing to commit (working directory clean)

And what git add says. Even less (nothing)

/path/to/root/dir $ git add a/b/c
artfulrobot
  • 20,637
  • 11
  • 55
  • 81
  • did you check the .gitignore file in the higher level directory? – jonathanbell May 25 '12 at 17:43
  • yes, it has nothing in it that would match the path. – artfulrobot May 25 '12 at 17:44
  • What is the message you get when you `git add`? What does `git status` show exactly? – another.anon.coward May 25 '12 at 17:45
  • I've put that info in the post. – artfulrobot May 25 '12 at 17:48
  • 1
    Was it a submodule? http://stackoverflow.com/questions/1260748/how-do-i-remove-a-git-submodule – Sebastian Celis May 25 '12 at 17:50
  • Just to clarify the status output is after `git add` right? I am not aware if `git add` really displays any message as such normally. How about the output of `git log a/b/c`? – another.anon.coward May 25 '12 at 17:51
  • @another.anon.coward: `git add` outputs nothing. `git log a/b/c` outputs nothing. @Sebastian: if it was a submodule I never knew and it was mine all along; i.e. I didn't `git submodule init` because your comment is the first I've read about git submodule! – artfulrobot May 25 '12 at 18:06
  • 2
    Just to be clear there are files in that directory? – bluesman May 25 '12 at 18:14
  • Rather than `a/b/c` having been simply another (nested) repository, might it have been a git submodule? – twalberg May 25 '12 at 18:42
  • **@bluesman:** yes there are five files. a/b/c is a drupal module of my own making. There's a .css a .js a .info a .module and a .html file. **@twalberg:** no, it was never a submodule (unless git creates them automatically without telling you). There is no .gitmodules file, nothing in the .git/config file to hint at that either. – artfulrobot May 25 '12 at 18:48

6 Answers6

34

I have no idea what the problem was or how it arose (v. annoying), but here is how I fixed it, in case anyone else gets stuck:

git rm --cached a/b/c
git commit -m "removed phantom a/b/c dir"
git add a/b/c
git commit -m "finally able to add a/b/c"

Interestingly git log a/b/c only lists the "finally able..." commit. git show HEAD^ (the "removed phantom..." commit says

-Subproject commit c311ccdc91c8be66019aa138d1c4af49a6b7a81c

So it looks like it was treating it specially some how. I'm going to have to read up more on subprojects and/or submodules.

artfulrobot
  • 20,637
  • 11
  • 55
  • 81
  • Sounds like there is/was something in the Index, independent of any missing .gitmodules file in the super project, that believed the directory was a tracked submodule. It would be useful to discover why. – Philip Oakley May 26 '12 at 20:28
  • Check for a .gitmodules in the upper repo. The file should list all of the submodules that that repo thinks it has (including the one you deleted ;-) – Philip Oakley May 28 '12 at 21:49
  • Thanks for reporting this. Just ran into the same issue. – Hephaestus Sep 22 '16 at 21:58
  • 3
    You're a life saver. Thank you. – Mapsy Jul 23 '17 at 13:33
  • This happened to me also. It happened after I did a git add from the subdirectory rather than the top level of the repo. I'm not sure if that is related. Your fix worked. Thank you. – bgreen-litl Jun 29 '20 at 14:15
4

Do you have a global gitignore file ? (Check with git config core.excludesfile)

ksol
  • 11,835
  • 5
  • 37
  • 64
4

Appears that an empty directory basically does not exist for Git. And if all files in a directory match .gitignore patterns, then git treats the directory as empty.

I had this happen on a directory with a foo.tgz file, and at the root of the repo the .gitignore file had the pattern *.tgz. Very frustrating that the git check-ignore command doesn't report something like "hey noob the directory looks empty to me".

Next time try this:

git check-ignore strangely-ignored-directory/*

That output just may solve the mystery.

chrisinmtown
  • 3,571
  • 3
  • 34
  • 43
  • This helped. It said that directory was marked as a submodule. running `git rm --cached strangely-ignored-directory` (no trailing slash) fixed it. – fred Mar 14 '21 at 23:43
2

Use git add -f to force git to add files it is currently ignoring.

gsganden
  • 21
  • 1
  • That will add all ignored folders including vendor/, which most people probably do not want to do. In my case it didn't fix the problem either which is the same as the OPs – fred Mar 14 '21 at 23:32
  • This doesn't add the files permanently, and the problem will re-occur on the next branch. – IgorGanapolsky Jun 08 '21 at 19:07
1

Summary: Are your .gitignore files (Including any in the directory itself) ignoring everything the directory contains. If so Git will treat it as empty and not list it)

I had a similar problem. I discovered that the directory that was being ignored contained a .gitignore file (Though not a .git directory). Upon removing it git was suddenly interested in it again and listed it as an untracked file/directory in git status.

When I restored the original contents of the .gitignore by pasting them back from earlier in the terminal git again doesn't see or list the directory as untracked.

Through trial and error I discovered that I could delete everything from the .gitignore except the following entries:

.factorypath
.springBeans

If both of those lines are in the .gitignore file then the containing directory (Which is not the root of the git repository) is not seen by git.

This raises more questions than it answers for me.

Here is a listing of the directory, but I cannot see a pattern.

total 72
drwxr-xr-x@ 10 stephen  staff    320 30 Aug 19:04 .
drwxr-xr-x  17 stephen  staff    544 30 Aug 18:59 ..
drwxr-xr-x   2 stephen  staff     64 25 Jun 15:00 .apt_generated
-rw-r--r--   1 stephen  staff   1341 25 Jun 15:00 .classpath
-rw-r--r--   1 stephen  staff  18665 28 Jun 10:44 .factorypath
-rw-r--r--@  1 stephen  staff     25 30 Aug 19:12 .gitignore
-rw-r--r--   1 stephen  staff   1094 28 Jun 10:44 .project
drwxr-xr-x   7 stephen  staff    224 25 Jun 15:00 .settings
-rw-r--r--   1 stephen  staff    479 28 Jun 15:43 .springBeans
drwxr-xr-x   6 stephen  staff    192  2 Jul 12:41 target
spl
  • 510
  • 4
  • 15
  • Okay, I have quickly discovered the likely answer at least to *my* manifestation. The .gitignore file in the directory was adding nodes to ignore such that everything in the directory was being ignored (The other nodes being ignored by other higher .gitignore files). Thus there's nothing special about the contents listed except that they help to ignore everything in the directory and because of Git's attitude to empty directories it doesn't list it anymore. – spl Aug 30 '18 at 18:23
1

This problem occurred for me just now because I had used create-react-app to initialise a client app inside a top-level folder todo controlled by git:

cd todo
npx create-react-app client --template typescript

Under the hood this created a folder client that had a .git directory and also a .gitignore file. It was like a git subproject.

When I added the project via git add -i to source control, a weird empty directory pointer was created but with no contents.

I used the command

git rm --cached client

...which removed the weird empty directory/pointer that git was storing. But when I tried to add the folder I got this message:

~/depot/todo $ git add client
warning: adding embedded git repository: client
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint: 
hint:   git submodule add <url> client
hint: 
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint: 
hint:   git rm --cached client
hint: 
hint: See "git help submodule" for more information.

To fix this I removed the git artifacts and from then I could commit as normal.

rm -Rf client/.git
rm -f client/.gitignore
git add client
git commit -m "Add missing client content"
Sez
  • 1,275
  • 11
  • 26