使用 .gitignore 将目录列入白名单 [英] Whitelist directories with .gitignore

查看:128
本文介绍了使用 .gitignore 将目录列入白名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to have two directories in one git repository. My method of choice is to create a git repository in a top-level directory and then use .gitignore to whitelist the two directories of interest. Requirements are within the comments of my attempted .gitignore file:

# 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 文件?

How to write a .gitignore file that whitelists two directories?

上述 gitignore 文件基于 .gitignore 忽略白名单文件夹

The above gitignore file is based on .gitignore ignoring whitelisted folder

这是我测试 .gitignore 的目录:

Here is the directory I tested .gitignore on:

$ 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 文件的方法:

Here is how I tested the .gitignore file:

$ 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 文件:

This .gitignore file whiteslists the directories A and B/B1, and excludes .txt files:

#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

这篇关于使用 .gitignore 将目录列入白名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆