.gitignore and git rm --cached wont keep files fro

2020-06-17 06:11发布

I have an android project that has a test module that will remove and recreate .apk files in a subdirectory in the git directory.

I have added these to .gitignore (the directory, the files explicitly, and *.apk ) and then I have done

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

and git tells me:

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

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

标签: git
3条回答
不美不萌又怎样
2楼-- · 2020-06-17 06:40

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 where this 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...

...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
查看更多
该账号已被封号
3楼-- · 2020-06-17 06:41

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.

查看更多
家丑人穷心不美
4楼-- · 2020-06-17 06:53

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.

查看更多
登录 后发表回答