906

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.

edwardmlyte
  • 15,937
  • 23
  • 58
  • 83
user3280133
  • 9,267
  • 3
  • 13
  • 15
  • 13
    1. what files are indexed although they shouldn't? 2. did you add the .gitignore after you added these files to the repo? – Ahmad Aug 21 '14 at 21:36
  • 3
    @Ahmad In multiple projects the `/bin/` folder still shows when I do a `git status`. I haven't changed those files since I added the `.gitignore` – user3280133 Aug 21 '14 at 21:38
  • 3
    "since I added the .gitignore" - so you added the .gitignore after adding the files? – Ahmad Aug 21 '14 at 21:40
  • @Ahmad You are correct. Will it not work until they are updated and then changed again? – user3280133 Aug 21 '14 at 21:42
  • 4
    possible duplicate of [.gitignore not working](http://stackoverflow.com/questions/11451535/gitignore-not-working) – Liam Dec 19 '14 at 15:04
  • 5
    Possible duplicate of [How to make Git "forget" about a file that was tracked but is now in .gitignore?](https://stackoverflow.com/questions/1274057/how-to-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore) – Lore Mar 08 '18 at 09:00
  • I know it's very old topic already, but just in case someone stumbles upon the same stupid problem I had: double check that the name of your file is exactly `.gitignore`, because if it's `gitignore` (without dot) - it won't work. ;) If it helped: you're welcome. – Constantine Ketskalo May 12 '20 at 18:57

21 Answers21

2172

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

Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • Most important part of your comment was that the already committed stuff also needs to be removed from your project, otherwise it will not be ignored. So concluding: step 1) Add lines to your local .gitignore step 2) Remove your redundant folders (like bin/obj) and commit changes – Hutjepower Oct 08 '15 at 09:07
  • I am assuming `-rf` is the same as `-r` here? – Jay Oct 27 '16 at 12:22
  • 4
    or you could just try getting reverting the files/folders that you added to .gitignore using git checkout folder_to_ignore/* – Zahra Feb 07 '17 at 19:22
  • 1
    @Greenish but that wouldn't work if they have already been committed, would only work if they've been staged, which in case the folder was already in the .gitignore shouldn't happen (unless you force add) :) – Ahmad Feb 07 '17 at 21:30
  • 2
    does not work for me when I push the changes to github. Says Everything is up to date !!!! – Dr. Younes Henni Nov 04 '17 at 14:24
  • @Dr.YounesHenni what does it show when you do `git status` after running the two commands? – Ahmad Nov 04 '17 at 18:11
  • 1
    Sorry Ahmed I fixed my issue by deleting the gitignore file and pulling and pushing again to the remote repo. I no longer remember what it said when I type git status. My issue is fixed. Thanks a lot. – Dr. Younes Henni Nov 04 '17 at 20:28
  • 9
    I think it's better git add a 'git ignore' command to simplify this. Thus we could just use `git ignore path/file` at anytime. – Til Feb 19 '18 at 05:44
  • 1
    My files in node_modules/ were untracked and the ones i cared about were added and committed. When i did `git rm -rf --cached` then `git add .` the add command added all of my node_modules/* even when it says to ignore them in my .gitignore – Coty Embry Nov 09 '18 at 16:11
  • I had to do the following (1st answer): https://stackoverflow.com/questions/10744305/how-to-create-gitignore-file – Coty Embry Nov 09 '18 at 16:18
  • Also didn't work for me, the cache folder is still showing in changed files... I've done exactly your solution. – prk_001 Apr 28 '20 at 11:01
  • @prk_001 what cache folder? have you checked whether your gitignore pattern is right? – Ahmad Apr 29 '20 at 00:20
  • 20
    Or use `git rm -rf --cached foldername` and `git add foldername` if you want to remove files from the folder `foldername`. That way you don't have to commit all other files – cwallenwein Jun 15 '21 at 08:12
  • 1
    It would be hellpful if an explanation of what -rf does is included within the answer. – MasayoMusic Jun 29 '21 at 01:07
  • It works but it also removes your git configuration, at least in my case it happened. So had to re-configure git. – first Dec 28 '22 at 11:23
  • This requires a subsequent code commit. It should not require a code commit just git to ignore a file. This is a hack. – arnoldbird Jan 29 '23 at 20:17
  • @arnoldbird you don't know how git works and that's not my fault – Ahmad Feb 18 '23 at 14:10
376

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"
Liam
  • 27,717
  • 28
  • 128
  • 190
Nicolas
  • 5,249
  • 3
  • 19
  • 20
102

@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

Vasiliki
  • 143
  • 9
sultanmyrza
  • 4,551
  • 1
  • 30
  • 24
61

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

Garrett Simpson
  • 1,471
  • 2
  • 17
  • 18
  • 6
    I quite like this approach - seems less destructive, even if it's not considered "proper". – theyetiman Feb 22 '17 at 11:01
  • I've often used this when dealing with less experienced developers that commit files, then wonder why their modified gitignore doesn't work. This is definitely the easiest and safest way of dealing with it. – Jag Apr 25 '23 at 14:23
27

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

  • this solution is really useful if you just want to remove 1 file (eg. setting file) from remote repository, but you still want to keep it in your local repository. – Wild Teddy Jan 07 '22 at 06:57
24

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.

Andrew Wynham
  • 2,310
  • 21
  • 25
  • 3
    UTF16 was the culprit for me. Opened VS code > opened .gitignore > opened the command prompt > "Change file Encoding" > "UTF-8" > Save file – Michael Jan 03 '20 at 17:57
  • 2
    Created files in PowerShell using `"blah blah" > file.txt` (`echo`), but files were UTF16. Fix: `$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'` – JoePC Sep 30 '20 at 23:58
15

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

Abraham
  • 12,140
  • 4
  • 56
  • 92
  • Uhh. This is a headache. I will just let the node_modules be in peace – Gilbert Jul 06 '21 at 14:01
  • 2
    @Gilboot that folder will mess up your entire experience if you include it in your repository. It will take too long hour to push changes, and the size of your repository will be unbearably huge, especially the number of files. making this change will save your days. if you don't have notepad++ you can simply open the file with normal notpad and use SaveAs, then to the left of the "Save" button, you can set the encoding to "UTF-8" and save the .gitignore file. this will make it work too – Abraham Jul 06 '21 at 14:33
  • 1
    Had the same problem. I recreated the .gitignore file with regular Notepad and saved as UTF-8. – Migisha Jul 28 '21 at 04:21
  • 1
    Same problem. I deleted/recreated the file using vscode main editor and it worked. Probably could have just changed the file format though. – RoboticRenaissance Aug 22 '21 at 18:34
  • 1
    Thank you!! I was scratching my head a lot before finding your answer! It worked like a charm! – OCarneiro Jul 10 '23 at 13:31
9

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.

S Saad
  • 111
  • 1
  • 3
6

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.

Ozesh
  • 6,536
  • 1
  • 25
  • 23
  • 1
    I had to do the above in combination with the accepted answer to make things work properly. Really sneaky problem, since .gitignore looks ok in VS editor, Notepad++ etc at first glance, but apparently the Windows implementation of Git can't handle Unix line breaks in a Windows environment. – Anders Sep 29 '20 at 14:25
5

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

026
  • 379
  • 3
  • 5
4

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
Ben Pritchard
  • 508
  • 6
  • 11
2

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.

DimaSan
  • 12,264
  • 11
  • 65
  • 75
user1889992
  • 355
  • 3
  • 7
  • 2
    Please ignore my comment above. Someone at work did something so wrong in Git, it caused duplicate folders and I was not aware for quite a while. Thanks. – user1889992 Dec 07 '16 at 14:01
2

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
Daniel Katz
  • 2,271
  • 3
  • 25
  • 27
2

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
user3251882
  • 922
  • 1
  • 11
  • 21
1

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
Wasonic
  • 39
  • 6
1

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.

Vasiliki
  • 143
  • 9
1

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.

RoboticRenaissance
  • 1,130
  • 1
  • 10
  • 17
0

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

0

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.

Sep
  • 147
  • 8
0

I'm a beginner and I had the same problem last night (on Windows).
I discovered that your .gitignore file must not have .txt extension.

Here is how I resolved it.

  1. Open any directory and go to menu bar, click view, and then check 'file name extensions'
  2. After that you edit the name of your .gitignore file by deleting .txt extention.
  3. As result you'll have a text file .gitignore without .txt extesion.
0

1- create new branch from last update branch 2- manuel delete caches files 3- update .gitignore + push to remote without caches 4- now generate again the caches files no problem

Abdallah Mahmoud
  • 491
  • 1
  • 5
  • 8