我有以下結構的項目:Git:我如何忽略子目錄中的匹配目錄?
/.
/src
/project1
/bin
/obj
/project2
/bin
/obj
/tools
/tool1
/bin
我可以添加到我的.git /信息什麼/排除忽略下的「SRC」所有斌/ OBJ目錄?我不想明確指定每個項目名稱。
我有以下結構的項目:Git:我如何忽略子目錄中的匹配目錄?
/.
/src
/project1
/bin
/obj
/project2
/bin
/obj
/tools
/tool1
/bin
我可以添加到我的.git /信息什麼/排除忽略下的「SRC」所有斌/ OBJ目錄?我不想明確指定每個項目名稱。
嘗試增加這幾行的.gitignore文件:
src/*/bin
src/*/obj
我相信你應該能夠添加
src/*/bin/*
到的.gitignore,以及任何與該模式匹配將被忽略。
不適用於`1.7.0.2.msysgit.0`。 – 2010-08-25 14:33:52
最明顯的辦法是把這些東西加到src/.gitignore
:
obj/
bin/
這種忽略是在任何路徑目錄調用obj
,或從src目錄向下調用一個名爲bin
的目錄。如果你有一些obj
和bin
目錄futher樹倒鋸齒狀項目層次
喜歡的東西在頂層.gitignore
src/*/obj/
可能無法正常工作。
這裏是展示在行動中忽略規則快速測試shell腳本:
#!/bin/sh
mkdir src
mkdir tools
mkdir src/project1
mkdir src/project2
mkdir tools/tool1
mkdir src/project1/bin
mkdir src/project1/obj
mkdir src/project2/bin
mkdir src/project2/obj
mkdir tools/tool1/bin
touch testfile
touch src/testfile
touch tools/testfile
touch src/project1/testfile
touch src/project2/testfile
touch tools/tool1/testfile
touch src/project1/bin/testfile
touch src/project1/obj/testfile
touch src/project2/bin/testfile
touch src/project2/obj/testfile
touch tools/tool1/bin/testfile
git init
add_empty() { touch "$1" && git add "$1"; }
add_empty dummy
add_empty src/dummy
add_empty tools/dummy
add_empty src/project1/dummy
add_empty src/project2/dummy
add_empty tools/tool1/dummy
git status
printf 'obj/\nbin/\n' >src/.gitignore && git add src/.gitignore
git status
的第一狀態未跟蹤文件部分是:
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# src/project1/bin/
# src/project1/obj/
# src/project1/testfile
# src/project2/bin/
# src/project2/obj/
# src/project2/testfile
# src/testfile
# testfile
# tools/testfile
# tools/tool1/bin/
# tools/tool1/testfile
並添加的.gitignore文件後:
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# src/project1/testfile
# src/project2/testfile
# src/testfile
# testfile
# tools/testfile
# tools/tool1/bin/
# tools/tool1/testfile
作爲一個測試來證明git不忽略文件obj
和bin
但進一步忽略obj
和bin
目錄下層次結構運行此腳本後:
#!/bin/sh
mkdir src/project3
touch src/project3/testfile && git add src/project3/testfile
touch src/project3/obj
touch src/project3/bin
mkdir src/subdir
mkdir src/subdir/proj
touch src/subdir/proj/testfile && git add src/subdir/proj/testfile
mkdir src/subdir/proj/obj
mkdir src/subdir/proj/bin
touch src/subdir/proj/obj/testfile
touch src/subdir/proj/bin/testfile
新的未跟蹤文件是:
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# src/project1/testfile
# src/project2/testfile
# src/project3/bin
# src/project3/obj
# src/testfile
# testfile
# tools/testfile
# tools/tool1/bin/
# tools/tool1/testfile
接受的答案並沒有爲我工作。
> git --version
git version 1.7.0.2.msysgit.0
似乎正斜槓不與在.gitignore
文件msysgit工作。這工作。
*\bin
*\obj
然而,將匹配排除名爲bin
或obj
過任何文件,這是不太可能是一個問題,除了當它。下面來解決這個(是的,正斜槓在這種情況下工作):
bin/
obj/
*.user
*.suo
這其中.gitignore
文件放在文件夾下的層次結構在不同深度相匹配的文件。請注意,當我爲特定的子文件夾添加前綴時,上述功能無效,所以我直接將其放在我的Source
文件夾中。
作爲一個Visual Studio用戶(推測OP也是bin/obj參考),排除.user
和.suo
文件也很好。
模式具有以下格式:
一個空行相匹配的任何文件,因此它可以作爲可讀性分離。
以#開頭的行作爲註釋。
可選的前綴!否定了這種模式;任何先前模式排除的匹配文件將再次包含在內。如果否定模式匹配,這將覆蓋較低優先模式源。
如果模式以斜線結尾,則爲了以下說明將其刪除,但它只會與目錄找到匹配項。換句話說,foo /會匹配目錄foo和它下面的路徑,但不會匹配常規文件或符號鏈接foo(這與pathpec在git中的工作方式一致)。
如果該模式不包含斜槓/,git會將其視爲shell glob模式,並檢查與相對於.gitignore文件位置的路徑名的匹配(相對於工作樹的頂層if而不是來自.gitignore文件)。
否則,git會將該模式視爲適合fnmatch(3)使用FNM_PATHNAME標誌使用的shell glob:模式中的通配符不會與路徑名中的/匹配。例如,「Documentation/*。html」與「Documentation/git.html」匹配,但不匹配「Documentation/ppc/ppc.html」或「tools/perf/Documentation/perf.html」。
前導斜槓匹配路徑名的開頭。例如,「/*.c」與「cat-file.c」匹配,但不匹配「mozilla-sha1/sha1.c」。
,一旦添加了我的困惑是,你把什麼口罩就可以了,文件將保留在倉庫中,除非強行拆除,所以已經增加了原料,編譯Visual Studio解決方案,我不得不清理倉庫發行:
git rm --cached */obj/*
git rm --cached */lib/*
git rm --cached *.user
git rm --cached *.suo
git rm --cached *ReSharper
然後,添加到了的.gitignore:
*/*/bin
*/*/obj
*.user
*.suo
*ReSharper*
則承諾:
git add .
git commit -m "removed user files and binaries from repository"
它只是工作...謝謝:) – 2009-09-18 19:53:38
用`1.7.0.2.msysgit.0`不適合我。我發佈了一個適合我的答案。 – 2010-08-25 14:40:39
@德魯:你的答案似乎更方便,因爲它與所有目錄相匹配,所以我投了贊成票。 這就是說,我不知道爲什麼這個答案不適合你,因爲我目前使用這種技術沒有問題。也許你的git版本是舊的? – 2010-08-25 18:30:47