Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git treats folder as file and ignores all subfolders and -files

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?

like image 245
Sherlock Avatar asked Nov 15 '16 09:11

Sherlock


People also ask

Does Git work on folders?

All of the local git "stuff" is placed in the . git folder in your "local repos" a.k.a. "working folder/directory". Folder/directory here is a folder that can contain other folders/directories - as well as files - indeed it will always contain a . git folder, otherwise it would not be a repository.

What files should not be stored in git?

You shouldn't store credentials like usernames, passwords, API keys and API secrets. If someone else steals your credentials, they can do nasty things with it.

How do I push subfolders to github?

initialize a new repo to serve as the child repo. move everything from the subdirectory of the parent repo work tree to the child repo work tree. commit the child repo. replace the subdirectory in the parent repo with a submodule reference.


2 Answers

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.

like image 192
Chris Avatar answered Oct 19 '22 03:10

Chris


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

like image 33
Ctrl-Zed Avatar answered Oct 19 '22 03:10

Ctrl-Zed