git - 使用 .gitignore 的白名单目录

标签 git whitelist subdirectory

我想在一个 git 存储库中有两个目录。 我选择的方法是在顶级目录中创建一个 git 存储库,然后使用 .gitignore 将感兴趣的两个目录列入白名单。 要求在我尝试的 .gitignore 文件的注释中:

# Blacklist everything
*
# Whitelist directories of interest and their contents
# stage files in directory A/ recursively
!A/
!A/*
# stage files in directory B/B1/ recursively (without staging files in B/)
!B/B1/
!B/B1/**
# Whitelist files of interest
# stage only the .md files (not .txt files)
!*.md

如何编写将两个目录列入白名单的 .gitignore 文件?

上面的gitignore文件是基于.gitignore ignoring whitelisted folder

这是我测试 .gitignore 的目录:

$ find A B C
A
A/a.md
A/a.txt
A/A1
A/A1/a1.md
A/A1/a1.txt
B
B/b.md
B/b.txt
B/B1
B/B1/b1.md
B/B1/b1.txt
C
C/c.md

这是我测试 .gitignore 文件的方式:

$ find A B C            #list all paths
$ git init
                        #test .gitignore:
$ git add . --dry-run   #show what would be staged
                        #edit gitignore and test again

预期输出:

$ git add . --dry-run
add 'A/A1/a1.md
add 'A/a.md
add 'B/B1/b1.md

最佳答案

此 .gitignore 文件将目录 A 和 B/B1 列入白名单,并排除 .txt 文件:

#exclude files and directories in top directory
/*

#include the A directory
!/A

#include the B directory
!/B
#exclude files and directories in B
/B/*
#include the B/B1 directory
!/B/B1

#ignore .txt files
*.txt

它基于 http://git-scm.com/docs/gitignore 中的最后一个示例

关于git - 使用 .gitignore 的白名单目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28229353/

相关文章:

mobile - phonegap 1.3.0 白名单是什么状态?

git - 是否可以撤消 `git checkout -- .` 以检索未暂存的更改

git - BitBucket 无法推送, "fatal: The remote end hung up unexpectedly"

git - 如何摆脱git中的 merge 提交

.htaccess - Codeigniter 2.1.4安装在子目录中

Python统计目录及其所有子目录中的文件

file - 如何从基于 SwiftUI 文档的应用程序将多个文件保存在包文件夹中?

git - 如何创建自己的 Git 分支来工作?

apache - Apache白名单反向代理

python - 限制对 AWS lambda flask 应用程序的访问