Git add all subdirectories
Solution 1
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
Simple solution:
git rm --cached directory
git add directory
Solution 3
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
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
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.
Josh Bradley
I am a Computer Science graduate student at the University of Maryland. There's never enough time in the day, but I try to keep up to pace with everything that's going on in both the CS and Math world. I've been programming for years now and I love to learn new languages. I've had experience with most of the "big contenders" out there including Python, Java, C, and C++. Even though I probably ask way too many questions on stack overflow, I hope to one day to give back to the community more than I've taken (knowledge).
Updated on May 27, 2021Comments
-
Josh Bradley almost 3 years
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 asDir1
. 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 seeDir2
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 inDir2
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. -
Josh Bradley about 11 yearsJust tried this. Still not working. I did git add . git commit git push origin master Am I missing something?
-
Kalle Pokki about 11 yearsAssuming 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 dogit add -f .
instead ofgit add .
. -
Josh Bradley about 11 yearsI 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.
-
Knows Not Much over 9 yearsgit add . does not work and it does not recursively add the sub directories and its content.
-
changtung over 8 yearsit can add ignored files and do a mess in repository.
-
cage rattler almost 5 yearsYou may need to open a fresh command window for this to work consistently.
-
Ranielle Canlas over 4 yearstried this on Jenkins and I received
fatal: Paths with -a does not make sense.
-
Rémi Benoit over 3 years
git add ./JS
is the correct usage. It will add all the files in the directory and all subdirectories still caring about ignored files. -
GCloony about 3 yearsweird issues arise when subdirs contain .git yes :) for dir in
find . -name ".git"
; do rm -rf $dir; done -
T.S almost 2 years"git add -f ." worked like a charm here. Thanks @KallePokki