2016-02-29 139 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

相關問題