Git 添加所有子目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14620863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:24:02  来源:igfitidea点击:

Git add all subdirectories

gitrepositorygit-add

提问by Josh Bradley

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 -Awith no success. For simplicity sake, say I initialized my git repository as Dir1. Then I have the following directory structure of files.

我在将文件夹及其所有子目录添加到我的 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

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 Dir2in 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 Dir2did 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.

我的真实文件有跨越 5-6 层深的子目录,那么是否有一个 git 命令将每个子目录中的所有文件添加到我的存储库中?现在,当我从手册页提出建议时,git add Dir1/\*我可以Dir2在我的回购中看到,但它显示为一个绿色文件夹,我无法打开它,这让我相信所有文件/文件夹都Dir2没有得到添加。任何帮助将不胜感激。我是一个新的 git 用户(使用它不到一周),所以尽量让你的说明保持在初学者的水平。

回答by Kalle Pokki

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.

而在存储库的根目录中。它将添加所有内容。如果你这样做git add *,它只会添加文件*指向。单点指的是目录。

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 .gitignorefile.

如果您的目录或文件在执行上述命令后没有添加到 git index/repo,请记住检查它是否被.gitignore文件中的 git 标记为已忽略。

回答by Shashwat Gupta

Simple solution:

简单的解决方案:

git rm --cached directory
git add directory

回答by PaulB

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

如果子目录本身是 git 存储库,您也可能会遇到问题 - 即 . 有一个 .git 目录 - 检查ls -a.

To remove go to the subdirectory and rm .git -rf.

要删除转到子目录和rm .git -rf.

回答by Ian Wright

Also struggled, but got it right typing

也很挣扎,但打字正确

git add -f ./JS/*

git add -f ./JS/*

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

其中 JS 是我的文件夹名称,其中包含子文件夹和文件

回答by Adam Ruhl

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.

我不能确定是否是这种情况,但对我来说似乎有问题的是在某些子目录中有 .gitignore 文件。同样,我不能保证这一点,但是在删除这些之后一切正常。

回答by samato

Most likely .gitignore filesare 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:

最有可能的.gitignore文件小号在作怪。请注意,.gitignore 文件不仅可以出现在 repo 的根级别,还可以出现在任何子级别。您可以从根级别尝试此操作以找到它们:

find . -name ".gitignore"

找 。-名称“.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.

也可能涉及子模块。检查“.gitmodules”文件的违规目录。

回答by Ash Kumar

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

我之前看到过这个问题,当我尝试添加的(子)文件夹的名称以“_Something_”开头时

I removed the underscores and it worked. Check to see if your folder has characters which may be causing problems.

我删除了下划线并且它起作用了。检查您的文件夹是否包含可能导致问题的字符。