2009-07-06 229 views

回答

19

尝试增加这几行的.gitignore文件:

src/*/bin 
src/*/obj 
+0

它只是工作...谢谢:) – 2009-09-18 19:53:38

+0

用`1.7.0.2.msysgit.0`不适合我。我发布了一个适合我的答案。 – 2010-08-25 14:40:39

+0

@德鲁:你的答案似乎更方便,因为它与所有目录相匹配,所以我投了赞成票。 这就是说,我不知道为什么这个答案不适合你,因为我目前使用这种技术没有问题。也许你的git版本是旧的? – 2010-08-25 18:30:47

1

我相信你应该能够添加

src/*/bin/* 

到的.gitignore,以及任何与该模式匹配将被忽略。

+1

不适用于`1.7.0.2.msysgit.0`。 – 2010-08-25 14:33:52

8

最明显的办法是把这些东西加到src/.gitignore

obj/ 
bin/ 

这种忽略是在任何路径目录调用obj,或从src目录向下调用一个名为bin的目录。如果你有一些objbin目录futher树倒锯齿状项目层次

喜欢的东西在顶层.gitignoresrc/*/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不忽略文件objbin但进一步忽略objbin目录下层次结构运行此脚本后:

#!/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 
16

接受的答案并没有为我工作。

> git --version 
git version 1.7.0.2.msysgit.0 

似乎正斜杠不与在.gitignore文件msysgit工作。这工作。

*\bin 
*\obj 

然而,将匹配排除名为binobj过任何文件,这是不太可能是一个问题,除了当它。下面来解决这个(是的,正斜杠在这种情况下工作):

bin/ 
obj/ 
*.user 
*.suo 

这其中.gitignore文件放在文件夹下的层次结构在不同深度相匹配的文件。请注意,当我为特定的子文件夹添加前缀时,上述功能无效,所以我直接将其放在我的Source文件夹中。

作为一个Visual Studio用户(推测OP也是bin/obj参考),排除.user.suo文件也很好。


gitignore specification

模式具有以下格式:

  • 一个空行相匹配的任何文件,因此它可以作为可读性分离。

  • 以#开头的行作为注释。

  • 可选的前缀!否定了这种模式;任何先前模式排除的匹配文件将再次包含在内。如果否定模式匹配,这将覆盖较低优先模式源。

  • 如果模式以斜线结尾,则为了以下说明将其删除,但它只会与目录找到匹配项。换句话说,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”。

2

,一旦添加了我的困惑是,你把什么口罩就可以了,文件将保留在仓库中,除非强行拆除,所以已经增加了原料,编译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"