Git treats folder as file and ignores all subfolders and -files
Asked Answered
H

4

9

I'm having a strange issue with Git. I use Composer to include TCPDF in my application, but I can't get it added to my repository.

Here's what I do step-by-step:

enter image description here

The status quo. These are just plugins; it's lacking the 'tcpdf' folder I need.

Now I run Composer to have it install the additional directory.

enter image description here

In this picture, it's already clear that something is wrong. Composer downloaded all files and folders and TCPDF is functioning on my machine, but only the tcpdf folder is shown red. The subfolders are not! I go to the command line and do 'git add .', so that it adds all files and folders. Now it looks like this:

enter image description here

Still wrong.

The wrong status is reflected by git status:

enter image description here

I'm really stuck here. I have no idea how to tell Git that this isn't a file but a folder. My repo is perfect, but if my colleague updates he only gets this file, without the folder content.

How do I fix this?

Hit answered 15/11, 2016 at 9:47 Comment(3)
What's the output when you do git add . in the tcfdf folder? Is there a .gitignore file in there? Does git status --ignored show the files in the folder as being ignored? If so, then you'll either need to stop ignoring them, or forcefully add them with git add -f PATHS_TO_ADD.Pothunter
Could tcpdf be a symlink, by any chance?Commix
git add . in the folder makes no difference, there's no .gitignore, git status --ignored doesn't show any files and tcpdf is no symlink.Hit
H
1

No idea what was wrong, but I copied the entire contents of tcpdf to a new folder called test, committed that folder and its contents successfully, removed the tcpdf-'file', committed, then renamed the test-folder to tcpdf and committed again.

Strange but all good now.

Hit answered 15/11, 2016 at 10:12 Comment(0)
L
14

Since you've done your copy-contents-then-move workaround there's no easy way to test this, but I suspect your tcpdf/ directory contained its own .git/.

Git handles nested directories by creating a special "gitlink" file in the parent repository. When you copied the contents from the tcpdf/ directory into the test directory you could easily have missed the hidden .git/ directory, which means that directory is no longer a nested repository.

With Composer, this could have happened if you used composer require --prefer-source or similar. This is the default if you're using an unstable version.

While some developers like to track the vendor/ directory that Composer creates it's not recommended:

Should I commit the dependencies in my vendor directory?

The general recommendation is no. The vendor directory (or wherever your dependencies are installed) should be added to .gitignore/svn:ignore/etc.

The best practice is to then have all the developers use Composer to install the dependencies. Similarly, the build server, CI, deployment tools etc should be adapted to run Composer as part of their project bootstrapping.

That page goes on to explain why this recommendation exists. I recommend reading the entire page. If you follow this best practice it won't matter if you use --prefer-dist or --prefer-source.

In general, both the composer.json and composer.lock files should be tracked.

Lyudmila answered 15/11, 2016 at 12:45 Comment(0)
D
14

Late to the party, as usual, but I stumbled here so others might too.

I had this issue(1) when I accidentally left a .git folder in a sub-folder within my git repo.

I solved it by clearing my local git's cache: git rm -rf --cached . (notice the full-stop at the end!).

See git rm -? for info on the flags.

(1) git thought a folder was a single file

Determinate answered 15/5, 2020 at 12:45 Comment(1)
Same problem - also late to the party. Had copied folder to existing repo without deleting .git folder first ! My solution was just to delete the child .git folderConvey
H
1

No idea what was wrong, but I copied the entire contents of tcpdf to a new folder called test, committed that folder and its contents successfully, removed the tcpdf-'file', committed, then renamed the test-folder to tcpdf and committed again.

Strange but all good now.

Hit answered 15/11, 2016 at 10:12 Comment(0)
M
1

What you had in tcpdf directory was git's submodule. To check if it was true, you can checkout back to commit prior to removing tcpdf-'file' and run git submodule status, also root folder of your worktree should contain .gitmodules. Maybe there was the a healthy reason to have a submodule in your repo.

Megaron answered 5/12, 2016 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.