Gitignore not working
Asked Answered
P

21

988

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.

Poyang answered 21/8, 2014 at 21:34 Comment(7)
1. what files are indexed although they shouldn't? 2. did you add the .gitignore after you added these files to the repo?Playback
@Playback In multiple projects the /bin/ folder still shows when I do a git status. I haven't changed those files since I added the .gitignorePoyang
"since I added the .gitignore" - so you added the .gitignore after adding the files?Playback
@Playback You are correct. Will it not work until they are updated and then changed again?Poyang
possible duplicate of .gitignore not workingEtch
Possible duplicate of How to make Git "forget" about a file that was tracked but is now in .gitignore?Hooey
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.Farewell
P
2325

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

Playback answered 21/8, 2014 at 21:46 Comment(16)
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 changesCypriot
I am assuming -rf is the same as -r here?Lampedusa
or you could just try getting reverting the files/folders that you added to .gitignore using git checkout folder_to_ignore/*Sensorimotor
@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) :)Playback
does not work for me when I push the changes to github. Says Everything is up to date !!!!Incessant
@Dr.YounesHenni what does it show when you do git status after running the two commands?Playback
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.Incessant
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.Prompt
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 .gitignoreSnuffle
I had to do the following (1st answer): stackoverflow.com/questions/10744305/…Snuffle
Also didn't work for me, the cache folder is still showing in changed files... I've done exactly your solution.Handful
@Handful what cache folder? have you checked whether your gitignore pattern is right?Playback
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 filesArlo
It would be hellpful if an explanation of what -rf does is included within the answer.Obrien
It works but it also removes your git configuration, at least in my case it happened. So had to re-configure git.Chrissie
This requires a subsequent code commit. It should not require a code commit just git to ignore a file. This is a hack.Siren
C
404

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"
Cayla answered 1/10, 2014 at 9:1 Comment(8)
Be sure to merge your branches back to master or move your pointer because when I tried this I lost both of my feature branches. Fortunately I did not lose any work, all of my most recent updates from the working branch were reset to the master branch but depending on your setup this behavior may be undesirable.Kicksorter
How would you go about using this command to remove a folder out of the cache instead of a file?Doelling
by git rm -r --cached . you deleted my previus changes, thank youSchoof
He did say to first commit any outstanding code changes...Bodgie
git rm --cached filename worked like a charm! Thanks!Efflorescence
This didn't work for meShawanda
This only removed the files that were meant to be ignored for a few folders. Completely misses .uasset files in most of the folders.Franklinfranklinite
It should not require a new commit just to get .gitignore to work. This is a hack.Siren
C
120

@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

Cerium answered 5/10, 2020 at 10:10 Comment(1)
Also in Step2 if you have a directory instead of a file you can use: git rm -r --cached directorynameIndoor
B
68

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.

Brina answered 26/10, 2016 at 17:10 Comment(3)
I quite like this approach - seems less destructive, even if it's not considered "proper".Mccann
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.Perforation
Had the same in Android Studio and your steps fixed it. Seems git related.Dusky
P
30

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

Pikeman answered 30/7, 2021 at 11:21 Comment(1)
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.Gnarled
A
26

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.

Aguayo answered 2/10, 2019 at 19:2 Comment(2)
UTF16 was the culprit for me. Opened VS code > opened .gitignore > opened the command prompt > "Change file Encoding" > "UTF-8" > Save fileTitulary
Created files in PowerShell using "blah blah" > file.txt (echo), but files were UTF16. Fix: $PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'Wyoming
S
17

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

Sandglass answered 29/3, 2021 at 11:8 Comment(5)
Uhh. This is a headache. I will just let the node_modules be in peaceHighclass
@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 tooSandglass
Had the same problem. I recreated the .gitignore file with regular Notepad and saved as UTF-8.Rhizogenic
Same problem. I deleted/recreated the file using vscode main editor and it worked. Probably could have just changed the file format though.Ripping
Thank you!! I was scratching my head a lot before finding your answer! It worked like a charm!Beeline
B
11

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.

Betsey answered 21/6, 2017 at 9:47 Comment(0)
C
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.

Chrystalchryste answered 28/3, 2018 at 11:38 Comment(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.Endotoxin
O
5

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

Orontes answered 29/11, 2016 at 19:11 Comment(0)
A
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
Arianna answered 3/4, 2020 at 3:22 Comment(0)
S
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.

Soothfast answered 6/12, 2016 at 21:47 Comment(1)
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.Soothfast
S
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
Seduction answered 27/5, 2020 at 17:11 Comment(0)
M
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
Monochrome answered 25/6, 2021 at 13:25 Comment(0)
R
2

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.

Ripping answered 22/8, 2021 at 18:35 Comment(1)
This fixed it. For some reason the file created with node_modules > .gitignore did not work. Re-creating the .gitignore file with a text editor solved the issue. Weird!Docent
B
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).

Bonin answered 3/5, 2021 at 11:15 Comment(0)
M
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.

Manaker answered 3/8, 2021 at 16:33 Comment(0)
C
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

Copal answered 11/4, 2021 at 10:5 Comment(2)
Funny, that's how I broke mine.Ripping
@Ripping is something wrong with this? Please let me inform.. thank you :)Copal
R
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.

Rehnberg answered 10/1, 2022 at 7:7 Comment(0)
G
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.
Greisen answered 3/11, 2022 at 8:59 Comment(0)
F
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

Flashback answered 29/5, 2023 at 11:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.