【问题标题】:Whitelist directories with .gitignore使用 .gitignore 将目录列入白名单
【发布时间】:2015-03-29 13:01:48
【问题描述】:

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

# 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

【问题讨论】:

    标签: git whitelist subdirectory


    【解决方案1】:

    此 .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中的最后一个示例

    【讨论】:

      【解决方案2】:

      您可以使用几个 .gitignore 文件来解决您的问题:

      # .gitignore
      *
      !.gitignore
      !A/
      !A/*
      !B/
      !B/*
      !*.md
      
      # A/.gitignore
      !*
      
      # B/.gitignore
      !*
      

      【讨论】:

        【解决方案3】:

        这是我所做的,基于交叉引用问题中的信息和此问题中的目录设置:

        $ mkdir junk
        $ cd junk
        $ git init
        Initialized empty Git repository in /Users/jleffler/soq/junk/.git/
        $ mkdir -p A/A1 B/B1 C
        $ > A.md
        $ > A/a.md
        $ > A/a.txt
        $ > A/A1/a1.md
        $ > A/A1/a1.txt
        $ > B/b.md
        $ > B/b.txt
        $ > B/B1/b1.md
        $ > B/B1/b1.txt
        $ > C/c.md
        $ cat > .gitignore
        *
        !A/
        !A/**
        !B/
        !B/**
        !*.md
        $ git status
        On branch master
        
        Initial commit
        
        Untracked files:
          (use "git add <file>..." to include in what will be committed)
        
            A.md
            A/
            B/
        
        nothing added to commit but untracked files present (use "git add" to track)   
        $ git add A.md A B
        $ git status
        On branch master
        
        Initial commit
        
        Changes to be committed:
          (use "git rm --cached <file>..." to unstage)
        
            new file:   A.md
            new file:   A/A1/a1.md
            new file:   A/A1/a1.txt
            new file:   A/a.md
            new file:   A/a.txt
            new file:   B/B1/b1.md
            new file:   B/B1/b1.txt
            new file:   B/b.md
            new file:   B/b.txt
        
        $
        

        添加AB后,里面的所有文件都在等待添加。请注意.gitignore 文件包含目录(尾部斜杠不是强制性的,AFAICT),然后是名称!A/**。双星是递归搜索的关键。


        我使用了git reset(实际上从未进行过提交),并与.gitignore 一起使用。我不清楚这些目标目前是自洽的。但是,这个修改了.gitignore文件:

        *
        !A/
        #!A/**.md
        !B
        !B/B1
        #!B/B1/**.md
        !*.md
        B/b.md
        

        加上git add A.md A B 产量:

        $ git status
        On branch master
        
        Initial commit
        
        Changes to be committed:
          (use "git rm --cached <file>..." to unstage)
        
            new file:   A.md
            new file:   A/a.md
            new file:   B/B1/b1.md
        
        $
        

        您应该能够使用显示的主题的变体来获得您想要的结果。 !*.md 行跟踪所有 .md 文件。当然,B/b.md 行会忽略 B/b.mdC 中的 .md 文件被忽略,因为 C 被列入黑名单(通过 *)。

        【讨论】:

        • 乔纳森,对不起,我没有让要求更明确。您的 .gitingnore 很接近,但有两个缺点:1)它正在上演 B/b.md;有没有办法只暂存目录 A/ 和 B/B1/(不暂存 B/ 中的文件)? 2)它正在暂存.txt文件;有没有办法只暂存 .md 文件?
        • 我会调查的。请更新问题以在此处也清楚说明这些要点。您希望包含 B/b.md 是因为它是一个 .md 文件还是排除它是因为它位于 B 中?
        • "stage only the .md files (not .txt files)" 是全局的。
        猜你喜欢
        • 2021-01-27
        • 2015-01-04
        • 1970-01-01
        • 1970-01-01
        • 2021-12-08
        • 2017-10-05
        • 1970-01-01
        • 2012-10-06
        • 2012-05-15
        相关资源
        最近更新 更多