假设我有一个名为 REPO 的目录,其中包含启动的 git 存储库,在该目录中,有 2 个名为 sub1
的子目录。 , sub2
和一个“1.cpp
”文件。
sub1
有“2.cpp
”文件和其他文件。
sub2
有“3.cpp
”文件和其他文件,所以它会像这个图一样
现在我只想要我的存储库中的这些文件,而不需要任何目录
1.cpp
2.cpp
3.cpp
我知道如何忽略除 .cpp
之外的所有文件使用.gitignore
的文件但我不想sub1
和sub2
要包含在我的存储库中,我只想要 cpp
文件中,我搜索了很多但找不到答案,希望我能正确解释。
最佳答案
您可以移动它们:
cd /path/to/REPO
git mv sub1/2.cpp .
git mv sub2/3.cpp .
然后删除不需要的子文件夹。
但实际上,OP 的意思是:
I simply want Git to include all my .cpp files from all subdirectories and add them to the repository.
know how to ignore all files except .cpp files using .gitignore but I don't want sub1 and sub2 to be included in my repo, I only want the cpp files in them
如果您尚未添加任何文件,您的 .gitignore
将为
**
!**/
!*.cpp
然后 git add .
将仅包含 cpp 文件。
您忽略所有文件 (**
),然后排除文件夹 (!**/
):这样您就可以排除 .cpp
文件( !*.cpp
) 来自 gitignore 规则。
那是因为it is not possible to re-include a file if a parent directory of that file is excluded .
关于git - 如何将具有特定扩展名的文件从子目录添加到 git 存储库而不添加子目录本身?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48353919/