2016-02-29 100 views
3

我在这里搜索这个答案,但没有什么是相似的,或者我迷路了。我试图排除除一个文件夹以外的所有特定子文件夹。也许我可以手动完成所有路径,但我认为应该有一个聪明的方法。我的文件结构:Gitignore排除子​​文件夹(** /模式),除了特定的一个

.gitignore 
test/ 
    file1 
    file2 
    sim_case/ 
     file1 
     file2 
      include/ 
       file1 
       file2 

在的.gitignore我:

**/sim_case 

,那就是排除一切从sim_case,但我想包括文件夹“包括”。我尝试了一些变化,没有任何工作。

!**/sim_case/include 
!**/sim_case/include/ 
!**/sim_case/include/* 
!sim_case/include/ 
!sim_case/include/ 
!sim_case/include/* 

回答

0

我做了一些研究,它似乎是最好的选择,排除里面只有文件“sim_case”文件夹使用:

**/sim_case/* 
!**/sim_case/include 

,允许有更复杂的文件结构

test/sim_case/include 
test1/sim_case/include 
etc. 

来自“git status”的结果:

On branch master 

Initial commit 

Changes to be committed: 
    (use "git rm --cached <file>..." to unstage) 

    new file: .gitignore 
    new file: test/file1 
    new file: test/file2 
    new file: test/sim_case/include/file1 
    new file: test/sim_case/include/file2 
    new file: test1/file1 
    new file: test1/file2 
    new file: test1/sim_case/include/file1 
    new file: test1/sim_case/include/file2 
2

你的第一个假设**/sim_case是错误的。

test/sim_case/** 
!test/sim_case/include/** 

从字面上看,您不想忽略完整目录sim_case,而只是sim_case的内容,但include目录中的文件除外。

git status建议添加sim_case目录。这样做,那么:

$ git status 
On branch master 

Initial commit 

Changes to be committed: 
    (use "git rm --cached <file>..." to unstage) 

     new file: sim_case/include/.gitignore 
     new file: sim_case/include/file1 
     new file: sim_case/include/file2 

Untracked files: 
    (use "git add <file>..." to include in what will be committed) 

     .gitignore 
     file1 
     file2 
2
test/sim_case/* 
!test/sim_case/include 

忽略目录草率忽略在该目录中的一切,和git扫描忽略反向条目,并采取了第一场比赛,所以最后两个条目说:“忽略测试一切/ sim_case(但仍然扫描目录),除了test/sim_case/include“。

+1

这将随着2.8而改变。 http://stackoverflow.com/a/20652768/6309。 'test/sim_case'然后是'!test/sim_case/include'就足够了。 – VonC

相关问题