.gitignore 和 git rm --cached 不会阻止文件被跟踪
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7693130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.gitignore and git rm --cached wont keep files from being tracked
提问by browep
I have an android project that has a test module that will remove and recreate .apk files in a subdirectory in the git directory.
我有一个 android 项目,它有一个测试模块,该模块将在 git 目录的子目录中删除并重新创建 .apk 文件。
I have added these to .gitignore (the directory, the files explicitly, and *.apk ) and then I have done
我已将这些添加到 .gitignore (目录、明确的文件和 *.apk ),然后我完成了
git rm -r --cached src/main/Tests/bin
and git tells me:
和 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'
i then
我然后
git commit . -m "removed apk files"
I run the tests again which delete the files and the recreate them. I run git status and they show up as modified. It seems .gitignore is not working. Here is my .gitignore file
我再次运行测试,删除文件并重新创建它们。我运行 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
采纳答案by Sri Sankaran
I just tried your recipe and it worked for me.
我刚试过你的食谱,它对我有用。
~ % 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) %
Perhaps the gitconfig in some other directory (or a global directive) that's overriding what you have in the gitconfig you have stated. You did not mention wherethis gitconfig is located. See the man page for gitignore or the online help docs for the precedence of the various locations where you can specify ignore rules. The man pages say...
也许其他目录(或全局指令)中的 gitconfig 覆盖了您在 gitconfig 中声明的内容。你没有提到其中该gitconfig所在。请参阅 gitignore 的手册页或在线帮助文档,了解您可以指定忽略规则的各个位置的优先级。手册页说...
...the following order of precedence, from highest to lowest (within one level of precedence, the last matching pattern decides the outcome): Patterns read from the command line for those commands that support them.
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
...以下优先级顺序,从高到低(在一个优先级内,最后一个匹配模式决定结果): 从命令行读取的模式,用于支持它们的命令。
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
回答by Genjuro
I think you need to clean all your stagged files using:
我认为您需要使用以下方法清理所有交错文件:
git rm -r --cached .
git add -A
Then commit:
然后提交:
git commit -m ".gitignore"
But make sure you commit your changes before doing this.
但请确保在执行此操作之前提交更改。
回答by Adam Dymitruk
have you tried just
你有没有试过
*.apk
in your .gitignore? If they were tracked before, you will need to delete them and commit that before setting the .gitignore. Once they are out, add the entry in the .gitignore file and it should work.
在你的 .gitignore 中?如果之前跟踪过它们,则需要在设置 .gitignore 之前删除它们并提交。一旦它们出来,在 .gitignore 文件中添加条目,它应该可以工作。