Git add all subdirectories

183,808

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.

Share:
183,808
Josh Bradley
Author by

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, 2021

Comments

  • Josh Bradley
    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 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.

  • Josh Bradley
    Josh Bradley about 11 years
    Just tried this. Still not working. I did git add . git commit git push origin master Am I missing something?
  • Kalle Pokki
    Kalle Pokki about 11 years
    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 ..
  • Josh Bradley
    Josh Bradley about 11 years
    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.
  • Knows Not Much
    Knows Not Much over 9 years
    git add . does not work and it does not recursively add the sub directories and its content.
  • changtung
    changtung over 8 years
    it can add ignored files and do a mess in repository.
  • cage rattler
    cage rattler almost 5 years
    You may need to open a fresh command window for this to work consistently.
  • Ranielle Canlas
    Ranielle Canlas over 4 years
    tried this on Jenkins and I received fatal: Paths with -a does not make sense.
  • Rémi Benoit
    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
    GCloony about 3 years
    weird issues arise when subdirs contain .git yes :) for dir in find . -name ".git"; do rm -rf $dir; done
  • T.S
    T.S almost 2 years
    "git add -f ." worked like a charm here. Thanks @KallePokki