简体   繁体   中英

Git add all subdirectories

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.

Do,

git add .

Simple solution:

git rm --cached directory
git add directory

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

Also struggled, but got it right typing

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.

"

Most likely .gitignore file s<\/strong> 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:

-name ".gitignore"

Check the offending directories for ".gitmodules" files.

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

Check to see if your folder has characters which may be causing problems.

If for someone git add .<\/code> is not working (as in my case as well), use git add .\/*<\/code> 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

  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 Folders , Subfolders 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM