Gitignore not working

GitGitignore

Git Problem Overview


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 Solutions


Solution 1 - Git

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).

Solution 2 - Git

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"

Solution 3 - Git

@Ahmad's answer is working but if you just want to git ignore 1 specific file or few files do as @Nicolas suggests

Step 1

add filename to .gitignore file

Step 2

[remove filename (file path) from git cache

git rm --cached filename

Step 3

commit changes git add filename

git commit -m "add filename to .gitignore"

It will keep your git history clean because if you do git rm -r --cached . and add back all and commit them it will pollute your git history (it will show that you add a lot of files at one commit) not sure am I expressing my thought right but hope you get the point

Solution 4 - Git

After going down a bit of a bit of a rabbit hole trying to follow the answers to this question (maybe because I had to do this in a visual studio project), I found the easier path was to

  1. Cut and paste the file(s) I no longer want to track into a temporary location

  2. Commit the "deletion" of those files

  3. Commit a modification of the .gitignore to exclude the files I had temporarily moved

  4. Move the files back into the folder.

I found this to be the most straight forward way to go about it (at least in a visual studio, or I would assume other IDE heave based environment like Android Studio), without accidentally shooting myself in the foot with a pretty pervasive git rm -rf --cached . , after which the visual studio project I was working on didn't load.

Solution 5 - Git

I used something to generate common .gitignore for me and I ran into this. After reading @Ozesh answer I opened in VS Code because it has a nice indicator at bottom right showing type of line endings. It was LF so I converted to CRLF as suggested but no dice.

Then I looked next to the line endings and noticed it was saved using UTF16. So I resaved using UTF8 encoding an voila, it worked. I didn't think the CRLF mattered so I changed it back to LF to be sure and it still worked.

Of course this wasn't OPs issue since he had already committed the files so they were already indexed, but thought I'd share in case someone else stumbles across this.

TLDR; If you haven't already committed the files and .gitignore still isn't being respected then check file encoding and, make sure its UTF8 and if that doesn't work then maybe try messing with line endings.

Solution 6 - Git

In cmd window use below git command,

git rm --cached filename

explanation:

git-rm - Remove files from the working tree and from the index

--cached Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.

--from https://git-scm.com/docs/git-rm

Solution 7 - Git

I had done echo node_modules >> .gitignore and it didn't work.

for some reason, the terminal from vscode saves the file in UCS-2 LE BOM and git doesn't seem to accept that.

I opened and converted the file to UTF-8 using Notepad++

changing encoding in notepad++

It Works now.

I think they need to fix this since echo "filetoignore" >> .gitignore actually seems a handy thing to do

Solution 8 - Git

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.

Solution 9 - Git

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

Solution 10 - Git

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.

Solution 11 - Git

Also, comments have to be on their own line. They can't be put after an entry. So this won't work:

/node_modules  # DON'T COMMENT HERE (since nullifies entire line)

But this will work:

# fine to comment here
/node_modules

Solution 12 - Git

Adding my bit as this is a popular question.

I couldn't place .history directory inside .gitignore because no matter what combo I tried, it just didn't work. Windows keeps generating new files upon every save and I don't want to see these at all.

enter image description here

But then I realized, this is just my personal development environment on my machine. Things like .history or .vscode are specific for me so it would be weird if everyone included their own .gitignore entries based on what IDE or OS they are using.

So this worked for me, just append ".history" to .git/info/exclude

echo ".history" >> .git/info/exclude

Solution 13 - Git

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.

Solution 14 - Git

My problem was that I first created .gitignore on a Mac but was now working on a Windows machine. On Macs you use "/" in your file path. On Windows you use "\".

So in order for my .gitignore file to work I had be consistent in what symbol I used. In this case I had to use "/" for every file path I wanted to add, even if when copied it was like this: "file\path".

Lost multiple hours around this silliness (it really bugged me lol).

Solution 15 - Git

There are lot of answers on stackoverflow but what worked for me is not altogether in any particular one, I am jotting all the steps one by one that needs to be done here in a simple manner :

Pre --- Take a backup of repo just in case. (I did not but may make you feel safer)

  1. Make sure whether this is your first commit or earlier these files were add and thus have become cached.
  2. If yes , then git rm -r --cached . and git add . (do not miss the dot)
  3. If it still isn't working , open file in notepad

Go to File -> Save as Change the encoding to ANSI. (Some answers mistakenly say UTF-8 , but it doesn't work and wastes time needlessly)

Again git rm -r --cached .
    and git add . (do not miss the dot) 

Now do Git status and verify
    what we wanted has happened

Solution 16 - Git

Someone might find this useful. The problem for me was the format. For some reason, and without having made any changes, git didn't like the format of the contents of the file. Directories in the .gitignore were like this:

directory1/*
directory2/*
directory3/*

When I changed the format to

directory1/
directory2/
directory3/

the directories were again ignored and the only thing I needed to add/commit/push was the .gitignore.

Solution 17 - Git

I know this has a lot of answers already, but the way I ended up at this question was as follows...

I had done 'node_modules' > .gitignore before an npm install in a new repo and it wasn't until I changed the .gitignore (added '**/node_modules', '/node_modules', etc), and then went git status, that I realized it was in a Binary format, instead of a text format, and thus git didn't recognize it.

I solved my problem by deleting the .gitignore file and re-adding it in my editor.

Solution 18 - Git

well my mistake was confusing .ginignore with .idea/.gitignore i was doing everything with .idea/.gitignore and no change would be applied to git repository. hope this helps the people doing the same mistake as i did.

Solution 19 - Git

in my case

i used git-bash and write this command:

> echo [Enter_your_file_name.extension] >> .gitignore

then perform push in repo and it is working fine

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3280133View Question on Stackoverflow
Solution 1 - GitAhmadView Answer on Stackoverflow
Solution 2 - GitNicolasView Answer on Stackoverflow
Solution 3 - GitsultanmyrzaView Answer on Stackoverflow
Solution 4 - GitGarrett SimpsonView Answer on Stackoverflow
Solution 5 - GitAndrew WynhamView Answer on Stackoverflow
Solution 6 - Gituser16557645View Answer on Stackoverflow
Solution 7 - GitAbrahamView Answer on Stackoverflow
Solution 8 - GitS SaadView Answer on Stackoverflow
Solution 9 - Git026View Answer on Stackoverflow
Solution 10 - GitOzeshView Answer on Stackoverflow
Solution 11 - GitBen PritchardView Answer on Stackoverflow
Solution 12 - GitDaniel KatzView Answer on Stackoverflow
Solution 13 - Gituser1889992View Answer on Stackoverflow
Solution 14 - GitWasonicView Answer on Stackoverflow
Solution 15 - Gituser3251882View Answer on Stackoverflow
Solution 16 - GitVasilikiView Answer on Stackoverflow
Solution 17 - GitRoboticRenaissanceView Answer on Stackoverflow
Solution 18 - GitSepView Answer on Stackoverflow
Solution 19 - Gitdamanpreet singhView Answer on Stackoverflow