89

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.

Dom
  • 1,591
  • 5
  • 26
  • 32
Josh Bradley
  • 4,310
  • 11
  • 49
  • 78
  • 2
    `git add` of any directory is automatically recursive. Using `git add .` in the repo's top level should add everything in there. If it doesn't, `.gitignore` is in play (local or global). – Nevik Rehnel Jan 31 '13 at 07:55
  • did you check `.gitignore`? Somehow maybe your directories are ignored. – ogzd Jan 31 '13 at 09:58
  • 2
    what is the output of `git status --ignored`? – Chronial Jan 31 '13 at 11:17
  • 2
    Just to help others who see this question, if the directories you make don't have any files in them, they are not added by `git add .` You have to add some file in the directories for git to track them. – gonephishing May 29 '15 at 01:26

8 Answers8

112

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.

Wolverine
  • 1,634
  • 1
  • 15
  • 17
Kalle Pokki
  • 4,489
  • 2
  • 14
  • 17
  • 8
    Just tried this. Still not working. I did git add . git commit git push origin master Am I missing something? – Josh Bradley Jan 31 '13 at 08:06
  • 4
    Assuming your files are under Dir1, you should do `cd Dir1; git init; git add . ; git commit;` Nothing else is required. If you have .gitignore ignoring files in some directory and still want to add them, you need to do `git add -f .` instead of `git add .`. – Kalle Pokki Jan 31 '13 at 08:16
  • 5
    I tried "git add ." but it wasn't working. The next day, I went back in and tried and everything "just worked" so thanks! It must have been one of those midnight bugs that go away when the light comes back on. – Josh Bradley Feb 04 '13 at 06:54
  • 2
    git add . does not work and it does not recursively add the sub directories and its content. – Knows Not Much Sep 13 '14 at 21:21
  • You may need to open a fresh command window for this to work consistently. – cage rattler Jun 24 '19 at 20:22
  • tried this on Jenkins and I received `fatal: Paths with -a does not make sense.` – Ranielle Canlas Aug 01 '19 at 12:10
17

Simple solution:

git rm --cached directory
git add directory
Shashwat Gupta
  • 3,139
  • 23
  • 21
14

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.

PaulB
  • 20,984
  • 13
  • 54
  • 75
5

Also struggled, but got it right typing

git add -f ./JS/*

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

Ian Wright
  • 67
  • 1
  • 1
1

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.

Adam Ruhl
  • 31
  • 3
0

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.

samato
  • 1
  • 1
0

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.

AlBlue
  • 20,872
  • 14
  • 63
  • 85
Ash Kumar
  • 1
  • 2
0

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.

T.M15
  • 137
  • 1
  • 9