Git add all subdirectories

GitRepositoryGit Add

Git Problem Overview


I'm having trouble adding a folder and all of it's subdirectories to my git repository. I realized this is a very popular question after doing some googling and I've tried each suggestion with no luck, specifically the suggestion from the man page on git-add. I even tried git add -A with no success. For simplicity sake, say I initialized my git repository as Dir1. Then I have the following directory structure of files.

Dir1/file1-1.txt
Dir1/file1-2.txt
Dir1/Dir2/file2-1.txt
Dir1/Dir2/Dir3/file3-1.txt

My real files have subdirectories that span 5-6 levels deep, so is there a git command to add all the files in each subdirectory to my repository? Right now, when I do the suggestion from the man page git add Dir1/\* I can see Dir2 in my repo, but it shows up as a green folder and I can't open it, which leads me to believe that all the files/folders in Dir2 did not get added. Any help would be greatly appreciated. I'm a new git user (less than a week of using it), so try and keep your instructions at a beginner's level.

Git Solutions


Solution 1 - Git

Do,

git add .

while in the root of the repository. It will add everything. If you do git add *, it will only add the files * points to. The single dot refers to the directory.

If your directory or file wasn't added to git index/repo after the above command, remember to check if it's marked as ignored by git in .gitignore file.

Solution 2 - Git

Simple solution:

git rm --cached directory
git add directory

Solution 3 - Git

You can also face problems if a subdirectory itself is a git repository - ie .has a .git directory - check with ls -a.

To remove go to the subdirectory and rm .git -rf.

Solution 4 - Git

Also struggled, but got it right typing

git add -f ./JS/*

where JS was my folder name which contain sub folders and files

Solution 5 - Git

I can't say for sure if this is the case, but what appeared to be a problem for me was having .gitignore files in some of the subdirectories. Again, I can't guarantee this, but everything worked after these were deleted.

Solution 6 - Git

Most likely .gitignore files are at play. Note that .gitignore files can appear not only at the root level of the repo, but also at any sub level. You might try this from the root level to find them:

find . -name ".gitignore"

and then examine the results to see which might be preventing your subdirs from being added.

There also might be submodules involved. Check the offending directories for ".gitmodules" files.

Solution 7 - Git

I saw this problem before, when the (sub)folder I was trying to add had its name begin with "_Something_"

I removed the underscores and it worked. Check to see if your folder has characters which may be causing problems.

Solution 8 - Git

If for someone git add . is not working (as in my case as well), use git add ./* which included all files in all subdirectories. My directory structure is:

MainDirectory
|_.git
|_README
|_folder1
|   |_file1
|   |_file2
|   |_subfolder1
|   |    |_file3
|   |    |_file4
|   |_subfolder2
|        |_file5
|        |_file6
|_folder2 
|   |_file1
|   |_file2
|   |_subfolder1
|   |    |_file3
|   |    |_file4
|   |_subfolder2
|        |_file5
|        |_file6
|_otherfiles

doing git add ./* included everything inside one level depth or more while git add . was adding only files at current level.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJosh BradleyView Question on Stackoverflow
Solution 1 - GitKalle PokkiView Answer on Stackoverflow
Solution 2 - GitShashwat GuptaView Answer on Stackoverflow
Solution 3 - GitPaulBView Answer on Stackoverflow
Solution 4 - GitIan WrightView Answer on Stackoverflow
Solution 5 - GitAdam RuhlView Answer on Stackoverflow
Solution 6 - GitsamatoView Answer on Stackoverflow
Solution 7 - GitAsh KumarView Answer on Stackoverflow
Solution 8 - GitT.M15View Answer on Stackoverflow