Gitignore not working

2019-01-03 00:44发布

My .gitignore file isn't working for some reason, and no amount of Googling has been able to fix it. Here is what I have:

*.apk
*.ap_
*.dex
*.class
**/bin/
**/gen/
.gradle/
build/
local.properties
**/proguard/
*.log

It's in the directory master, which is my git repo. I'm running Git 1.8.4.2 because I'm on a MacBook running OSX 10.8.6.

标签: git gitignore
7条回答
可以哭但决不认输i
2楼-- · 2019-01-03 01:20

To untrack a single file that has already been added/initialized to your repository, i.e., stop tracking the file but not delete it from your system use: git rm --cached filename

To untrack every file that is now in your .gitignore:

First commit any outstanding code changes, and then, run this command:

git rm -r --cached .

This removes any changed files from the index(staging area), then just run:

git add .

Commit it:

git commit -m ".gitignore is now working"
查看更多
手持菜刀,她持情操
3楼-- · 2019-01-03 01:22

In my case whitespaces at the end of the lines of .gitignore was the cause. So watch out for whitespaces in the .gitignore!

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-03 01:23

The files/folder in your version control will not just delete themselves just because you added them to the .gitignore. They are already in the repository and you have to remove them. You can just do that with this:

(Remember to commit everything you've changed before you do this.)

git rm -rf --cached .
git add .

This removes all files from the repository and adds them back (this time respecting the rules in your .gitignore).

查看更多
地球回转人心会变
5楼-- · 2019-01-03 01:24

In my case it was a blank space at the beginning of the file which showed up clearly when I opened the file in Notepad, wasn't obvious in Visual Studio Code.

查看更多
你好瞎i
6楼-- · 2019-01-03 01:29

I solved my problem doing the following:

First of all, I am a windows user, but i have faced similar issue. So, I am posting my solution here.

There is one simple reason why sometimes the .gitignore doesn`t work like it is supposed to. It is due to the EOL conversion behavior.

Here is a quick fix for that

Edit > EOL Conversion > Windows Format > Save

You can blame your text editor settings for that.

For example:

As i am a windows developer, I typically use Notepad++ for editing my text unlike Vim users.

So what happens is, when i open my .gitignore file using Notepad++, it looks something like this:

## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore


# See https://help.github.com/ignore-files/ for more about ignoring files.

# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
*.dll
*.force
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs

If i open the same file using the default Notepad, this is what i get

## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. ## ## Get latest from  https://github.com/github/gitignore/blob/master/VisualStudio.gitignore # See https://help.github.com/ignore-files/ for more about ignoring files. # User-specific files *.suo *.user *.userosscache 

So, you might have already guessed by looking at the output. Everything in the .gitignore has become a one liner, and since there is a ## in the start, it acts as if everything is commented.

The way to fix this is simple: Just open your .gitignore file with Notepad++ , then do the following

Edit > EOL Conversion > Windows Format > Save

The next time you open the same file with the windows default notepad, everything should be properly formatted. Try it and see if this works for you.

查看更多
乱世女痞
7楼-- · 2019-01-03 01:33

Does git reset --hard work for anyone? I am not saying this is a good solution, it just seemed to work first time I tried.

查看更多
登录 后发表回答