Git add all subdirectories
Asked Answered
W

9

112

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.

Wivestad answered 31/1, 2013 at 7:50 Comment(4)
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).Emotionalism
did you check .gitignore? Somehow maybe your directories are ignored.Lode
what is the output of git status --ignored?Sappy
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.Apospory
A
131

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.

Allowable answered 31/1, 2013 at 7:56 Comment(7)
Just tried this. Still not working. I did git add . git commit git push origin master Am I missing something?Wivestad
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 ..Allowable
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.Wivestad
git add . does not work and it does not recursively add the sub directories and its content.Morehouse
You may need to open a fresh command window for this to work consistently.Lollop
tried this on Jenkins and I received fatal: Paths with -a does not make sense.Hydria
"git add -f ." worked like a charm here. Thanks @KallePokkiAntofagasta
I
32

Simple solution:

git rm --cached directory
git add directory
Isolecithal answered 18/4, 2018 at 8:29 Comment(0)
J
24

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.

Jaymie answered 10/8, 2016 at 22:20 Comment(1)
weird issues arise when subdirs contain .git yes :) for dir in find . -name ".git"; do rm -rf $dir; doneOmnipresent
H
7

Also struggled, but got it right typing

git add -f ./JS/*

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

Hecatomb answered 7/12, 2015 at 21:2 Comment(2)
it can add ignored files and do a mess in repository.Chauffer
git add ./JS is the correct usage. It will add all the files in the directory and all subdirectories still caring about ignored files.Jotun
C
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.

Curbstone answered 6/2, 2015 at 1:3 Comment(0)
D
1
  1. git add one file that's at the deepest layer of your file structure (the latest level of a Folder you want to add).
    For example:
    git add Folder/Subfolder-L1/Subfolder-L2/Subfolder-L3/...file123.md
  2. Then, git add --all :/.
    It will add all the FoldersSubfolders and files to the existing repo.
  3. git commit -m 'your commit.
  4. git push -u origin main, push to the remote origin. Voila!

References: Recursively add the entire folder to a repository

Dulles answered 6/8, 2022 at 4:34 Comment(0)
D
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.

Disposure answered 2/6, 2016 at 23:4 Comment(0)
O
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.

Oneself answered 16/6, 2016 at 21:37 Comment(0)
L
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.

Langouste answered 26/5, 2021 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.