.gitignore和git rm --cached不会阻止文件被跟踪

时间:2011-10-07 21:42:34

标签: git

我有一个Android项目,它有一个测试模块,可以删除并重新创建git目录子目录中的.apk文件。

我已将这些添加到.gitignore(目录,显式文件和* .apk)然后我已经完成了

git rm -r --cached src/main/Tests/bin

并且git告诉我:

rm 'src/main/Tests/bin/Tests-debug-unaligned.apk'
rm 'src/main/Tests/bin/Tests-debug.apk'
rm 'src/main/Tests/bin/Tests.ap_'
rm 'src/main/Tests/bin/classes.dex'

我然后

git commit . -m "removed apk files"

我再次运行测试,删除文件并重新创建它们。我运行git status并显示为已修改。看来.gitignore没有用。这是我的.gitignore文件

*ipr
*iml
*iws
.gitignore
out/
*apk
src/main/Tests/bin
src/main/Tests/bin/*
src/main/Tests/bin/Tests-debug-unaligned.apk

3 个答案:

答案 0 :(得分:28)

我认为你需要使用以下方法清理所有错误的文件:

git rm -r --cached .

git add -A

然后提交:

git commit -m ".gitignore"

但请确保在执行此操作之前提交更改。

答案 1 :(得分:1)

我刚试过你的食谱,它对我有用。

~ % mkdir deleteme
~ % cd deleteme
~/deleteme % mkdir src src/main src/main/Tests src/main/Tests/bin
~/deleteme % touch src/main/Tests/bin/blah.apk                   
~/deleteme % git init
Initialized empty Git repository in /Users/sri/deleteme/.git/
~/deleteme (master*) % touch src/main/hello.txt
~/deleteme (master*) % git add .
~/deleteme (master*) % git commit -m "init"
[master (root-commit) 0ef5764] init
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 src/main/Tests/bin/blah.apk
 create mode 100644 src/main/hello.txt
~/deleteme (master) % vi .gitignore
~/deleteme (master) % git rm -r --cached src/main/Tests/bin
rm 'src/main/Tests/bin/blah.apk'
~/deleteme (master*) % git commit -m "removed"
[master 5ca6456] removed
 0 files changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 src/main/Tests/bin/blah.apk
~/deleteme (master) % touch src/main/Tests/bin/blah.apk    
~/deleteme (master) % git status
# On branch master
nothing to commit (working directory clean)
~/deleteme (master) %  

也许在其他目录(或全局指令)中的gitconfig覆盖了你所说的gitconfig中的内容。你没有提到这个gitconfig所在的 where 。有关可以指定忽略规则的各个位置的优先级,请参阅gitignore的手册页或联机帮助文档。手册页说......

  

......以下顺序          优先级,从最高到最低(在一个优先级内,最后一个匹配          模式决定结果):   从命令行读取的模式支持那些命令   它们。

   o   Patterns read from a .gitignore file in the same directory as the path, or in any
       parent directory, with patterns in the higher level files (up to the toplevel of the
       work tree) being overridden by those in lower level files down to the directory
       containing the file. These patterns match relative to the location of the .gitignore
       file. A project normally includes such .gitignore files in its repository, containing
       patterns for files generated as part of the project build.

   o   Patterns read from $GIT_DIR/info/exclude.

   o   Patterns read from the file specified by the configuration variable core.excludesfile

答案 2 :(得分:0)

你试过吗

*.apk
你的.gitignore中的

?如果之前已经跟踪过,则需要删除它们并在设置.gitignore之前提交它们。一旦它们出来,在.gitignore文件中添加该条目,它应该可以工作。