[英]Git: How do I ignore matching directories in a sub directory?
I have a project with the following structure: 我有一个具有以下结构的项目:
/.
/src
/project1
/bin
/obj
/project2
/bin
/obj
/tools
/tool1
/bin
What can I add to my .git/info/exclude to ignore all bin/obj directories under "src"? 我可以添加到.git / info / exclude中以忽略“src”下的所有bin / obj目录? I don't want to explicitly specify each project name.
我不想明确指定每个项目名称。
Try adding these lines to your .gitignore file: 尝试将这些行添加到.gitignore文件中:
src/*/bin
src/*/obj
The accepted answer didn't work for me. 接受的答案对我不起作用。
> git --version
git version 1.7.0.2.msysgit.0
Seems that forward slashes don't work with msysgit in the .gitignore
file. 似乎正斜杠不能与
.gitignore
文件中的msysgit一起使用。 This works. 这有效。
*\bin
*\obj
However that will match exclude any files called bin
or obj
too, which isn't likely to be a problem, except for when it is. 然而,这将匹配排除任何名为
bin
或obj
文件,这不太可能是一个问题,除非它是什么时候。 The following gets around this (and yes, a forward slash works in this case): 以下解决了这个问题(是的,在这种情况下正斜杠工作):
bin/
obj/
*.user
*.suo
This matches files at different depths in the hierarchy beneath the folder in which the .gitignore
file is placed. 这将匹配放置
.gitignore
文件的文件夹下的层次结构中不同深度的文件。 Note that the above didn't work when I included a prefix for a specific subfolder, so I placed this in my Source
folder directly. 请注意,当我为特定子文件夹包含前缀时,上述操作无效,因此我将其直接放在我的
Source
文件夹中。
As a Visual Studio user (presumably the OP is too from bin/obj reference) it's also nice to exclude .user
and .suo
files. 作为Visual Studio用户(可能是OP来自bin / obj引用),排除
.user
和.suo
文件也很不错。
From the gitignore specification : 从gitignore规范 :
Patterns have the following format:
模式具有以下格式:
A blank line matches no files, so it can serve as a separator for readability.
空行不匹配任何文件,因此它可以作为可读性的分隔符。
A line starting with # serves as a comment.
以#开头的行作为注释。
An optional prefix !
一个可选的前缀! which negates the pattern;
否定了这种模式; any matching file excluded by a previous pattern will become included again.
之前模式排除的任何匹配文件将再次包含在内。 If a negated pattern matches, this will override lower precedence patterns sources.
如果否定模式匹配,则将覆盖较低优先级模式源。
If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory.
如果模式以斜杠结尾,则为了以下描述的目的将其删除,但它只会找到与目录的匹配项。 In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in git).
换句话说,foo /将匹配目录foo和它下面的路径,但是不匹配常规文件或符号链接foo(这与pathpec在git中的工作方式一致)。
If the pattern does not contain a slash /, git treats it as a shell glob pattern and checks for a match against the pathname relative to the location of the .gitignore file (relative to the toplevel of the work tree if not from a .gitignore file).
如果模式不包含斜杠/,git将其视为shell glob模式,并检查相对于.gitignore文件位置的路径名的匹配(相对于工作树的顶层,如果不是来自.gitignore)文件)。
Otherwise, git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will not match a / in the pathname.
否则,git将模式视为适合fnmatch(3)使用FNM_PATHNAME标志的shell glob:模式中的通配符与路径名中的/不匹配。 For example, "Documentation/*.html" matches "Documentation/git.html" but not "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html".
例如,“Documentation / * .html”匹配“Documentation / git.html”,但不匹配“Documentation / ppc / ppc.html”或“tools / perf / Documentation / perf.html”。
A leading slash matches the beginning of the pathname.
前导斜杠与路径名的开头匹配。 For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".
例如,“/ *。c”匹配“cat-file.c”但不匹配“mozilla-sha1 / sha1.c”。
The most obvious way would be to add these to src/.gitignore
: 最明显的方法是将这些添加到
src/.gitignore
:
obj/
bin/
This ignores any paths that are in a directory call obj
, or a directory called bin
from the src directory downwards. 这会忽略目录调用
obj
中的所有路径,或者从src目录向下忽略名为bin
的目录。
Something like src/*/obj/
in a top-level .gitignore
might not work if you have a jagged project hierarchy with some obj
and bin
directories futher down the tree. 顶级
.gitignore
中的src/*/obj/
类的东西可能不起作用,如果你有一个锯齿状的项目层次结构,在树下面有一些obj
和bin
目录。
Here's quick test shell script showing the ignore rule in action: 这是快速测试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
The untracked file section of the first status is: 第一个状态的未跟踪文件部分是:
# 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
And after adding the .gitignore file: 添加.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
As a test to prove that git isn't ignoring files called obj
and bin
but is ignoring obj
and bin
directories further down the hierarchy after running this script: 作为一个测试来证明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
The new untracked files are: 新的未跟踪文件是:
# 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
The confusion for me was that once added, whatever mask you put on it, the files will remain in the repository unless forcibly removed, so having added a raw, compiled visual studio solution, I had to clean the repository issuing: 令我感到困惑的是,无论你添加了什么掩码,除非强行删除,否则文件将保留在存储库中,因此添加了原始的,已编译的可视化工作室解决方案,我必须清理存储库发布:
git rm --cached */obj/*
git rm --cached */lib/*
git rm --cached *.user
git rm --cached *.suo
git rm --cached *ReSharper
then, added this to .gitignore: 然后,将其添加到.gitignore:
*/*/bin
*/*/obj
*.user
*.suo
*ReSharper*
then committed: 然后承诺:
git add .
git commit -m "removed user files and binaries from repository"
I believe you should be able to add 我相信你应该能够添加
src/*/bin/*
to .gitignore, and anything that matches the pattern will be ignored. 到.gitignore,以及与该模式匹配的任何内容都将被忽略。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.