git - 如何将具有特定扩展名的文件从子目录添加到 git 存储库而不添加子目录本身?

标签 git github bitbucket

假设我有一个名为 REPO 的目录,其中包含启动的 git 存储库,在该目录中,有 2 个名为 sub1 的子目录。 , sub2和一个“1.cpp ”文件。
sub1有“2.cpp”文件和其他文件。
sub2有“3.cpp ”文件和其他文件,所以它会像这个图一样

enter image description here

现在我只想要我的存储库中的这些文件,而不需要任何目录

1.cpp
2.cpp
3.cpp

我知道如何忽略除 .cpp 之外的所有文件使用.gitignore的文件但我不想sub1sub2要包含在我的存储库中,我只想要 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/

相关文章:

git - Github 公钥的公共(public) URL 是什么

git - 我应该如何构建我的 git 提交?

git stash list 或 git show ,返回 ( most +s +'/---' : 1: most: not found ) What is this?

Git:--force-with-lease 和多个 pushurls

java - 将现有 Maven Eclipse 项目上传到 GitHub

github - 收到致命警报 : protocol_version

git - Bitbucket 克隆私有(private) git 仓库

git - 安装 Gitzilla 时的 PyBugz 版本

javascript - Github Typescript --> Javascript 差异比较

git - 使用辅助私钥访问 bitbucket