Git 添加所有子目录

我有麻烦添加一个文件夹和它的所有子目录到我的 git 存储库。我意识到这是一个非常流行的问题后,做了一些谷歌和我已经尝试了每个建议,没有运气,特别是从 Git-add 的手册页的建议。我甚至尝试了 git add -A也没有成功。为了简单起见,假设我将 git 存储库初始化为 Dir1。然后我有以下文件的目录结构。

Dir1/file1-1.txt
Dir1/file1-2.txt
Dir1/Dir2/file2-1.txt
Dir1/Dir2/Dir3/file3-1.txt

我真正的文件有5-6级的子目录,所以是否有一个 git 命令将每个子目录中的所有文件添加到我的存储库中?现在,当我从 git add Dir1/\*的手册页提出建议时,我可以在我的回购文件中看到 Dir2,但是它显示为一个绿色的文件夹,我不能打开它,这使我相信 Dir2中的所有文件/文件夹都没有被添加。如果你能帮忙,我将不胜感激。我是一个新的 git 用户(使用不到一个星期) ,所以尽量把你的说明保持在初学者的水平。

201055 次浏览

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.

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.

Also struggled, but got it right typing

git add -f ./JS/*

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

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.

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.

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.

Simple solution:

git rm --cached directory
git add directory

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.

  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