1730

My .gitignore file seems to be being ignored by Git - could the .gitignore file be corrupt? Which file format, locale or culture does Git expect?

My .gitignore:

# This is a comment
debug.log
nbproject/

Output from git status:

# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       debug.log
#       nbproject/
nothing added to commit but untracked files present (use "git add" to track)

I would like debug.log and nbproject/ not to appear in the untracked files list.

Where should I start looking to fix this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matt Parkins
  • 24,208
  • 8
  • 50
  • 59
  • 93
    Make sure your `.gitignore` file uses `ANSI` or `UTF-8` encoding. If it uses something else like `Unicode BOM`, it's possible that Git can't read the file. – ADTC Dec 14 '17 at 12:39
  • 1
    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:01
  • 13
    @ADTC this was exactly the problem on my machine (Windows). I has used `echo "file" > .gitignore` in PowerShell, the file had a UCS-2 encoding! – MarioDS Apr 05 '18 at 14:52
  • 12
    `git rm --cached debug.log nbproject/` – Gayan Weerakutti May 29 '18 at 09:40
  • 8
    Why the first comment here is not an answer is beyond me – RedOrav Jul 02 '18 at 20:53
  • @RedOrav Amusingly, that was the answer to my question (I'm the op), although it is an answer much further down. – Matt Parkins Jul 10 '18 at 07:25
  • 2
    @MattParkins Ah I see it now, it still surprises me that such a simple and to the point answer is buried as a comment or that the actual accepted one is down there. Thanks! – RedOrav Jul 10 '18 at 22:08
  • If the files are indeed in the `Untracked files` list, make sure to chek out for trailing spaces (@Rawa [answer](https://stackoverflow.com/a/20736016/3565696)) – ederag Sep 14 '19 at 20:29
  • don't start ur folder names with `/folder`. That messed me up. Should have been `folder/`. – Charlie Parker Oct 13 '19 at 17:10
  • @ADTC nailed it. this was my issue. – Matt M Apr 06 '20 at 15:23
  • @GayanWeerakutti put simply, does that code let go of all historical baggage of code changes tracked by git for `debug.log`, `nbproject/` as well as sets the resp. files to be untracked by git ? – Sumax Jul 02 '22 at 07:03
  • @Sumax It only remove the file from the working directory and add that removal into the index. The history is preserved. – Gayan Weerakutti Aug 02 '22 at 18:10

38 Answers38

3506

Even if you haven't tracked the files so far, Git seems to be able to "know" about them even after you add them to .gitignore.

WARNING: First commit or stash your current changes, or you will lose them.

Then run the following commands from the top folder of your Git repository:

git rm -r --cached .
git add .
git commit -m "fixed untracked files"
Braian Coronel
  • 22,105
  • 4
  • 57
  • 62
Alin Huruba
  • 35,481
  • 2
  • 12
  • 9
  • 1
    i got a "Unknown option: -rm" when trying to run "git -rm -r --cached" – minovsky Aug 29 '12 at 04:01
  • 8
    I got "usage: git rm [options] [--] ..." printed when I tried "git rm -r --cached". If it matters, I'm using Git from within PowerShell after clicking "Tools->Open a shell here" in GitHub for Windows. – Soonts Oct 17 '12 at 13:17
  • 66
    on Windows: git rm . -r --cached __and__ git add . – Beachhouse Nov 13 '12 at 02:08
  • 2
    It should be: git rm -r --cached . and git add . The dot is important! It specifies to apply this to all files under the current directory, (so do this when in the top level git directory). I would edit this, but don't have enough reputation.... :) – quantka Jun 04 '13 at 14:49
  • 98
    Be aware to **commit** all your changes before, otherwise you will lose control on all the changed files!! – Cosmin Nov 27 '13 at 14:33
  • 49
    The first 3 commenters seem to have missed the dot in the end. This means every file. "git -rm -r --cached ." <-- notice the dot. – Christophe De Troyer May 02 '14 at 15:29
  • 3
    If rm this file, what happen with this file in server when I push it to server ? – ThangNguyen Jul 07 '14 at 08:29
  • If anyone is wondering, you can do this for certain files by replacing the `.` in this answer with the path name in both the `rm` and `add` commands. – scrowler Nov 18 '14 at 02:35
  • 1
    Works and is safe to push to server. It just fixes gitignore. – Tom Dec 02 '14 at 20:55
  • 1
    I'm not sure about the last step, just keep doing git rm and git add, eventually somehow it'll just work, but don't do commit, kinda of scary. – windmaomao Mar 03 '15 at 23:15
  • Can't this inadvertently change the Unix file permissions? – Adam Libuša Mar 20 '15 at 20:50
  • 20
    If you don't want to commit, a workaround is to put your changes onto the shelf: "git stash". Run the above commands. and run "git stash pop" – Ivan Voroshilin Apr 22 '15 at 10:59
  • 6
    At least on windows this commands lead to all files beeing committed (including files listet in .gitignore). So in my case it's not working/wrong. – syr Nov 27 '15 at 11:39
  • This just solved my issue without wasting a bunch of hours! This answer should be accepted as the solution too. – Cristian Jan 27 '16 at 07:01
  • TIL that copy pasting multiple lines into GIT bash, immediately executes all lines except the last one. – Edwin Stoteler Feb 01 '16 at 14:54
  • 4
    The suggested solution added the files I intended to ignore - the exact opposite of what I wanted... – CodeManX Mar 03 '16 at 10:31
  • git commit -m "fixed untracked files" . There should be a dot at last. – Arhat Baid May 26 '17 at 05:42
  • 1
    I would recommend use list of filenames instead `.` – Chase Choi Jan 04 '18 at 04:14
  • 1
    By George it actually worked. Now why does this issue even come up in the first place? seems like the default behavior should be to not let this happen... – russiansummer Mar 19 '18 at 20:07
  • If this affects such an enormous number of git users, this could be considered a bug or a feature request for intuitiveness. By the way, is this issue reproduced on machines other than Windows? Git installer for Windows has a checkbox to disable caching and it is enabled by default. – Aram Paronikyan Sep 13 '18 at 10:42
  • faced this issue in ubuntu 16.04. now working perfect – sam Oct 12 '18 at 08:27
  • This removed the .gitignore file from my project leaving me unable to add it back in, no idea why - please be careful with these commands. – JimmyM Oct 22 '18 at 15:22
  • 2
    This solution does not work! It's ridiculous how often this issue comes up and how difficult it is to address, even if one is an experienced git user. – arnoldbird Apr 03 '19 at 17:06
  • really nice , but i dont understand what is the meaning of first line git commit -m "fixed untracked files" – hosam hemaily May 13 '19 at 11:25
  • 2
    It's 2019, and this is still an issue, reproduced on ma mac. Fixed the issue and made me feel all warm inside. Tickled me just right. – Paul Tuckett Oct 17 '19 at 12:12
  • @arnoldbird you're right as it's imperative to be specific as to what files you're untracking eg. `git rm -r --cached debug.log nbproject` , while `git rm -r --cached .` would simple untrack all files in the project ⚠️ – Sumax Jul 02 '22 at 06:42
  • 1
    @hosamhemaily the line is a commit command which explains the file/sub-folders that were unindexed from history by `git rm -r --chached `. ️ [to understand `git add . && git commit`](https://stackoverflow.com/questions/8198105/how-does-git-store-files/8198276#8198276) – Sumax Jul 02 '22 at 06:53
  • Before doin that just type !.gitignore in your gitignore file. – Shourob Datta Aug 25 '22 at 09:48
  • The answer is correct in that if a folder or file exists in the repo then the changes will not be ignored by .gitignore. If you want to fix it without using git commands (like if you use Github Desktop) then cut and paste the file/folder out of your local directory. push it, then add them back in. That worked for me. – Matt Booth Nov 24 '22 at 00:06
361

If it seems like Git isn't noticing the changes you made to your .gitignore file, you might want to check the following points:

  • There might be a global .gitignore file that might interfere with your local one

  • When you add something into a .gitignore file, try this:

      git add [uncommitted changes you want to keep] && git commit
      git rm -r --cached .
      git add .
      git commit -m "fixed untracked files"
    
  • If you remove something from a .gitignore file, and the above steps maybe don't work, if you found the above steps are not working, try this:

      git add -f [files you want to track again]
      git commit -m "Refresh removing files from .gitignore file."
    
      // For example, if you want the .java type file to be tracked again,
      // The command should be:
      //     git add -f *.java
    
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
ifeegoo
  • 7,054
  • 3
  • 33
  • 38
  • 3
    Perhaps could you edit @AlinHuruba 's answer to add your step 3 ? – Benj Jul 04 '16 at 09:32
  • @Benj Thank you for your advice,I didn't find AlinHuruba's answer,I don't known what's the difference,so please tell me directly. – ifeegoo Oct 23 '17 at 01:42
  • 2
    The step 3 is what I needed. Most of the answer just tell how to remove the file which we want to untrack. Thanks for the opposite point of view. – Yeung Nov 01 '17 at 04:11
  • Hope this helps you! – ifeegoo Nov 01 '17 at 10:44
  • I'm happy that this can help you! – ifeegoo Mar 19 '18 at 03:35
  • Thanks @ifeegoo for the reply. Usually it works for me too but I am having issues within a latex proj, with the main .pdf, which I don't need to commit, as I am working remotely on overleaf. I also checked for empty spaces in the .gitignore ... do you have an idea what's going on? -- maybe you have any idea? (not sure if I should post it as another question) -- Thanks – jjrr Oct 24 '18 at 09:38
  • @jjrr .pdf file is one kind of large file for git,and you must pay attention to the file path and directory path. – ifeegoo Oct 26 '18 at 01:39
  • the file is 334 kb and the path seems correct ...plus, I am basically using the same .gitignore for other proj., where git is able to ignore the mail .pdf ...very weird – jjrr Oct 26 '18 at 07:32
235

Fixed. OK, I created the .gitignore file in Notepad on Windows and it wasn't working. When I viewed the .gitignore file on Linux it looked like organised gibberish - perhaps Notepad had written out Unicode rather than ASCII or whatever 8-bit is.

So I rewrote the file on my Linux box, and when I pulled it back into Windows it works fine! Hurrah!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matt Parkins
  • 24,208
  • 8
  • 50
  • 59
  • 86
    In Notepad simply choose the encoding "ANSI" in the "Save As" dialog. Or better yet, get a proper text editor - after all you're a programmer :) ... I can recommend "Programmer's Notepad", others prefer Notepad++ and there are literally hundreds more out there. – 0xC0000022L Jul 12 '12 at 12:41
  • 2
    hehe, yep, I should have created it in netbeans (which I was using at the time), eclipse or visual studio. I just called it from the commandline thinking it wouldn't make a difference. – Matt Parkins Jul 12 '12 at 13:39
  • 1
    We need to use linux/unix like relative path and not the windows path( i.e to use "/" instead of "\") even if you are using windows OS and save it in ANSI as mentioned in previous comment. – Dhaval Desai Jul 11 '13 at 09:32
  • 8
    My problem was similar - my .gitignore was using UTF8 with a [BOM](http://en.wikipedia.org/wiki/Byte_order_mark). Just saved it as UTF8 without a BOM and it magically started working. – Phil Mar 26 '14 at 21:07
  • This was my problem. But even if the file looks ok on Linux, if you changed it on Windows and it is being ignore, try do delete it and type it again on Linux. Worked for me. – Leo Medeiros Jun 16 '15 at 20:30
  • Best way for me is to just copy paste working .gitignore and edit that. Thank you. – aeroson Nov 07 '15 at 15:38
  • 15
    This was my problem as well. I had created the .gitignore file with "echo dirName > .gitignore" because Windows makes it such a pain to create files that start with ".". The encoding of the file created this way was unreadable by git, and it interpreted it as a binary file instead. Clicked "Encoding --> UTF-8" in Notepad++, save, done. – Laura Sep 07 '16 at 15:43
  • 4
    Had the same problem as @Laura in powershell - the file is by default saved as UTF16. – mrówa Sep 08 '16 at 10:19
  • Trailing spaces can get you there, too :) – mlvljr Nov 17 '16 at 23:28
  • I ran into this issue as well, copy/pasting the contents of the [VS .gitignore file](https://github.com/github/gitignore/blob/master/VisualStudio.gitignore) from Chrome was my problem. I should have directly saved the file somehow else. – David Peters Mar 06 '17 at 17:04
  • 1
    I had the same problem as Laura as well. Arghhhh Windows. -_- – hfaran Sep 13 '17 at 08:45
  • Going from a bash to occasionally using powershell - this command "echo someline >somefile" writes out UTF16. What the? – Danny Staple Feb 28 '18 at 22:54
  • 1
    For those of you using Notepad++, the option to select is `Encoding > Encode in ANSI`. – Grinn Mar 01 '18 at 17:00
  • 1
    Leave a comment if you're going to downvote the answer, (this one worked for me) – Matt Parkins May 21 '18 at 11:55
  • 1
    The common source of problem - line separators. They are two bytes on windows (0d 0a) and one byte in unix (0a). When you move windows file to unix 0d bytes will treat as significant on end of lines. Example: write bash script on windows with shebang [#!/usr/bin/env python3] and try to move to unix, [chmod +x] and run - there will be binary name [python3\x0d] and shell will fail. – Denis Barmenkov Apr 04 '20 at 11:09
  • @Grinn Better encode as UTF8 NO BOM. – ScienceDiscoverer Jul 24 '23 at 17:03
  • @0xC0000022L I do not approve on hating the Notepad++. In my opinion it is the best text editor for Windows. Period. Mostly because if you don't like something about it, you can fix it yourself. – ScienceDiscoverer Jul 24 '23 at 17:05
  • @ScienceDiscoverer good for you. And now perhaps look up the difference () between Notepad, a program that ships with Windows and -- while it has improved over the recent years -- can hardly be called a programmer's editor, and Notepad++ an excellent (small, fast, feature-rich) editor sharing the first few letters. At no point did I hate Notepad++ or discourage its use or even "spread" hate about it. While at the time of my comment I used PN2, I now prefer Notepad++ on systems where I can't have VSCodium or SublimeText. You're a bit late to the party, too ... – 0xC0000022L Jul 24 '23 at 20:59
125

Without adding another commit to your project, one line will be enough to make .gitignore work as it is supposed to:

git rm -r --cached debug.log nbproject

This will remove them from the repository, but still keep them physically. In plain English, it deletes any history of changes related to them, and also will not track their change in any future commit. You may find a better explanation here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
H Aßdøµ
  • 2,925
  • 4
  • 26
  • 37
  • 1
    Worked exactly as I wanted - I had only 1 file which was not tracked properly. I saved its contents in notepad, and did: `git rm -r --cached someFile.php` and it worked like a charm :) – ShayLivyatan Jul 20 '16 at 07:08
  • @FMFF This will remove it from the repository but still keep them physically, in plain English, it deletes any history of changes related to them, and also will not track their change in any future commit. A better explication you may found here: http://stackoverflow.com/questions/37279654/when-should-i-use-rm-git-rm-git-rm-cached-git-add – H Aßdøµ Apr 27 '17 at 01:20
  • 1
    I got a "fatal: pathspec 'debug.log' did not match any files" – Michael Aug 05 '19 at 13:38
52

Another cause of this issue is blank spaces or tabs before the statement:

Example:

# Be aware of the following:
 notWorkingIgnore.*
workingIgnore.*

And as pointed out by the comment below a trailing space can be an issue as well:

# Be aware of the following:
notWorkingIgnore.* #<-Space
workingIgnore.*#<-Nospace
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rawa
  • 13,357
  • 6
  • 39
  • 55
45

I noticed that the encoding of the .gitignore was having an effect--if the file was Unicode, it was ignored, if it was ASCII, it wasn't.

Process:

  1. Verify status: PS> git status
  2. Create a function to Get-FileEncoding
  3. Test .gitignore's encoding: PS> Get-FileEncoding .gitignore
  4. Change the encoding to ASCII: PS> Set-Content .gitignore -Encoding Ascii -Value (Get-Content .gitignore)
  5. Confirm: PS> git status
Community
  • 1
  • 1
craig
  • 25,664
  • 27
  • 119
  • 205
  • 6
    1000 thanks for this. I was creating the .gitignore file on my system using the `touch .gitignore` and `echo ".db" >> .gitignore` route via powershell. I found that the `echo ".db" >> .gitignore"` was setting the file encoding to UCS-2 LE-BOM, a quick conversion to ASCII and the gitignore started working. – B-Rad Feb 22 '16 at 05:03
  • 3
    You can try by skipping Step 2 & 3. – Aniket Bhansali Aug 30 '17 at 09:50
37

As with the other solutions, commit first and be aware that you will lose any un-committed changes.

I had better results with this:

git rm -r --cached .
git reset HEAD --hard
git status

Note that the status shouldn't have any modified files now.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Steven Stark
  • 1,249
  • 11
  • 19
24

In my case, it's because the files already exist in the repository and I'm trying to ignore it.

These are the things I did to fix the issue:

  • Copy the files to a temporary folder
  • Remove them from my project folder.
  • Commit the changes which remove those files from the repository
  • Re-added those files to my project folder

By then, any changes I made on those files were ignored.

I think you can't ignore files that already exist on the repository.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Edesa
  • 581
  • 7
  • 16
  • In my case, they were already staged. Only after doing what you suggest, then attempting to commit and seeing the warning that they had been staged and were removed did I realize that was the problem. – dudeNumber4 Aug 22 '19 at 14:24
20

All the answers here are actually workarounds. You need to create the .gitignore file before you run git init. Otherwise git will never know you need to ignore those files, because they have been tracked already.

echo .idea/ >> .gitignore
git init

If you develop on a daily basis, I advise you to add your habitual ignored files to your ~/.gitignore_global file. That way, git will already know which files you (meaning "your user", since it's a file in your home directory) usually ignore.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CESCO
  • 7,305
  • 8
  • 51
  • 83
  • Thank god... finally a solution which works for me. – Dbl Aug 30 '19 at 05:59
  • `~/.gitignore_global` is a configurable name. Run `git config --global core.excludesfile` first to see if you already have a file defined. If not, run `git config --global core.excludesfile ~/.gitignore_global`. – Noumenon Jan 08 '20 at 18:16
  • A workaround if you didn't create `.gitignore` first: rename the tracked file. `.gitignore` will be applied as if it were new. – Noumenon Jan 08 '20 at 19:14
17

Also check out the directory where you put .gitignore.

It should be in the root of your project:

./myproject/.gitignore

Not in

./myproject/.git/.gitignore
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Artem Zaytsev
  • 1,621
  • 20
  • 19
10

There's another issue with .gitignore that might happen, especially for a Windows user. Git does not like it when you name .gitignore (such as unity.gitignore).

You'll want to always name it .gitignore, or on Windows, .gitignore. as Windows thinks you are trying to rename it without a filename.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Goldblaze
  • 101
  • 1
  • 3
10

Whenever I encounter the situation that git is tracking a file that is listed in .gitignore, I use the following:

git update-index --skip-worktree <file_name>
Jesse
  • 121
  • 1
  • 6
9

Specifically for Windows users: If you have untracked files and clearing/removing the cached files is not working. Try opening PowerShell and converting the .gitignore file to UTF-8 encoding:

$Myfile = Get-Content .\.gitignore`
$Myfile | Out-File -Encoding "UTF8" .gitignore

You need to only do this once to encode the .gitignore file for that directory, and since the file is then encoded correctly, whenever you edit the file in the future it should work. I believe this is due to a glitch with GitHub not being about to read non UTF-8 encoding for a .gitignore file. As far as I'm aware this issue has not yet been resolved for Windows. It's not too big of a deal, just a pain to debug when it's not working.

Daniel Fisher lennybacon
  • 3,865
  • 1
  • 30
  • 38
maximusg
  • 143
  • 1
  • 7
8

I just ran into this issue. The content within my .gitignore file continued to appear in the list of untracked files.

I was using this to create the ignore file:

echo "node_modules" > .gitignore

It turns out that the double quotations were causing the issue for me. I deleted the ignore file and then used the command again without quotes, and it worked as expected. I did not need to mess with the file encoding. I'm on a Windows 10 machine using Cmder.

Example:

echo node_modules > .gitignore
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bprdev
  • 693
  • 9
  • 12
  • Weird, this echo statement fixed a problem I had locally where an ignored file that I deleted on accident and then recreated was showing up as a committable, untracked file. For some reason, even though this did not change my ignore file, it fixed my problem. – Patrick Oct 18 '17 at 21:39
  • this is the only thing that worked for me. mac user. i had to specify the files i wanted ignored manually back in .gitignore but that only took a few seconds. Thank you! – Pix81 Mar 25 '21 at 15:33
7

There are some great answers already, but my situation was tedious. I'd edited the source of an installed PLM (product lifecycle management) software on Win10 and afterward decided, "I probably should have made this a git repo."

So, the cache option won't work for me, directly. Posting for others who may have added source control AFTER doing a bunch of initial work AND .gitignore isn't working BUT, you might be scared to lose a bunch of work so git rm --cached isn't for you.

!IMPORTANT: This is really because I added git too late to a "project" that is too big and seems to ignore my .gitignore. I've NO commits, ever. I can git away with this :)

First, I just did:

rm -rf .git
rm -rf .gitignore

Then, I had to have a picture of my changes. Again, this is an install product that I've done changes on. Too late for the first commit of the pure master branch. So, I needed a list of what I changed since I'd installed the program by adding > changed.log to either of the following:

PowerShell

# Get files modified since date.
Get-ChildItem -Path path\to\installed\software\ -Recurse -File | Where-Object -FilterScript {($_.LastWriteTime -gt '2020-02-25')} | Select-Object FullName

Bash

# Get files modified in the last 10 days...
find ./ -type f -mtime -10

Now, I have my list of what I changed in the last ten days (let's not get into best practices here other than to say, yes, I did this to myself).

For a fresh start, now:

git init .
# Create and edit .gitignore

I had to compare my changed list to my growing .gitignore, running git status as I improved it, but my edits in .gitignore are read-in as I go.

Finally, I've the list of desired changes! In my case it's boilerplate - some theme work along with sever xml configs specific to running a dev system against this software that I want to put in a repo for other devs to grab and contribute on... This will be our master branch, so committing, pushing, and, finally BRANCHING for new work!

Vlad L.
  • 154
  • 1
  • 9
Neil Gaetano Lindberg
  • 2,488
  • 26
  • 23
6

OK, so in my case the accepted solution did not work, and what worked is described here:

Is Visual Studio 2013 ignoring your .gitignore file?

In short:

  • Close Visual Studio.
  • Navigate to your .git folder
  • Delete ms-persist.xml
  • Restart Visual Studio
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bartosz
  • 4,406
  • 7
  • 41
  • 80
6

For me none of the previous answers worked. I had to copy .gitignore text into the exclude.txt file found at

<Your-project-folder>\.git\info

Once done, refresh your changes and all the untracked files are gone. Commit as usual.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
raga
  • 899
  • 10
  • 14
6

I had this problem, with a .gitignore file containing this line:

lib/ext/

I just realized that in fact, this directory is a symbolic link to a folder somewhere else:

ls -la lib/ext/
lrwxr-xr-x 1 roipoussiere users 47 Feb  6 14:16 lib/ext -> /home/roipoussiere/real/path/to/the/lib

On the line lib/ext/, Git actually looks for a folder, but a symbolic link is a file, so my lib folder is not ignored.

I fixed this by replacing lib/ext/ by lib/ext in my .gitignore file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
roipoussiere
  • 5,142
  • 3
  • 28
  • 37
6

My issue was (as OP suggested) a corrupt .gitignore file. I didn't believe that it was and ignored the possibility until everything else failed. The corruption didn't show up in vi, but there were two bytes on the start of the file that caused the .gitignore file to be ignored. For me, these only showed up when I typed cat .gitignore, which showed:

��# Built application files
*.apk
*.ap_

# ...

I have no idea how these ended up there, but recreating the file fixed the issue. A hex analysis of the corrupt file showed the following:

user@dev ~/project/myproject $ xxd -b .gitignore
00000000: 11111111 11111110 00100011 00000000 00100000 00000000  ..#. .
00000006: 01000010 00000000 01110101 00000000 01101001 00000000  B.u.i.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pcdev
  • 2,852
  • 2
  • 23
  • 39
  • 3
    That looks like a UTF-16 byte order mark in a UTF-8-encoded file. https://en.wikipedia.org/wiki/Byte_order_mark – jsageryd Apr 04 '18 at 07:00
5

I had this same problem. I believe the issue was a CR vs. CR+LF discrepancy. I stashed things in my .gitignore using CMD (on Windows 7) and the following command:

Bad:

echo "file_to_be_ignored.py" >> .gitignore<br>
echo "*~" >> .gitignore

Etc.

The issue was that this command does not place the correct end-of-line marker for Git to recognize the newlines (either CR or CR+LF when Git expects the other). I solved the problem by manually replacing each newline in Vim (Vim to the rescue!) and it worked perfectly.

Try editing your .gitignore in Notepad++ or Vim (ideally). Even if the file looks like it's formatted correctly, try replacing the newlines. It sounds weird, I know, but it worked for me. :D

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
fictivelogic
  • 139
  • 2
  • 7
4

I've created .gitignore using echo "..." > .gitignore in PowerShell in Windows, because it does not let me to create it in Windows Explorer.

The problem in my case was the encoding of the created file, and the problem was solved after I changed it to ANSI.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ahmad
  • 5,551
  • 8
  • 41
  • 57
4

Another possible reasona few instances of Git clients running at the same time. For example, "git shell" + "GitHub Desktop", etc.


This happened to me. I was using "GitHub Desktop" as the main client, and it was ignoring some new .gitignore settings: commit after commit:

  1. You commit something.
  2. Next, commit: it ignores .gitignore settings. Commit includes lots of temporary files mentioned in the .gitignore.
  3. Clear Git cache; check whether .gitignore is UTF-8; remove files → commit → move files back; skip one commit – nothing helped.

Reason: the Visual Studio Code editor was running in the background with the same opened repository. Visual Studio Code has built-in Git control, and this makes for some conflicts.

Solution: double-check multiple, hidden Git clients and use only one Git client at a time, especially while clearing the Git cache.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Oleg Zarevennyi
  • 2,753
  • 1
  • 21
  • 21
3

One thing to also look at: Are you saving your .gitignore file with the correct line endings?

Windows:

If you're using it on Windows, are you saving it with Windows line endings? Not all programs will do this by default; Notepad++ and many PHP editors default to Linux line endings so the files will be server compatible. One easy way to check this, is open the file in Windows Notepad. If everything appears on one line, then the file was saved with Linux line endings.

Linux:

If you are having trouble with the file working in a Linux environment, open the file in an editor such as Emacs or nano. If you see any non-printable characters, then the file was saved with Windows line endings.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Beachhouse
  • 4,972
  • 3
  • 25
  • 39
3

One tricky thing not covered by the other answers here is that the .gitignore file won't work if you have inline comments, like this:

foo/bar # The bar file contains sensitive data so we don't want to make this public

So, if you do have comments like that, change them like this:

# The bar file contains sensitive data so we don't want to make this public
foo/bar
James
  • 1,394
  • 2
  • 21
  • 31
3

For me it was yet another problem. My .gitignore file is set up to ignore everything except stuff that I tell it to not ignore. Like such:

/*
!/content/

Now this obviously means that I'm also telling Git to ignore the .gitignore file itself. Which was not a problem as long as I was not tracking the .gitignore file. But at some point I committed the .gitignore file itself. This then led to the .gitignore file being properly ignored.

So adding one more line fixed it:

/*
!/content/
!.gitignore
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user8118328
  • 653
  • 1
  • 7
  • 10
2

It is also a possibility that you edited the .gitignore file with a sudo command. I encountered the same issue and while executing the commands: git status, I could still see the "should be ignored" files.

Upon editing with nano .gitignore instead of sudo nano .gitignore, I could see the correct reflection.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aviral sanjay
  • 953
  • 2
  • 14
  • 31
2

I too have the same issue on Ubuntu, I created the .gitignore from the terminal and it works for me

touch .gitignore

U.A
  • 2,991
  • 3
  • 24
  • 36
2

Mine wasn't working because I've literaly created a text document called .gitignore

Instead, create a text document, open it in Notepad++ then save as .gitignore

Make sure to pick All types (*.*) from the dropdown when you save it.


Or in gitbash, simply use touch .gitignore

Kari
  • 1,244
  • 1
  • 13
  • 27
1

Just remove the folder or file, which was committed previously in Git, by the following command. Then gitignore file will reflect the correct files.

    git rm -r -f "folder or files insides"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Atif Hussain
  • 880
  • 12
  • 19
1

If you are a Notepad++ user, try doing the following:

Open your .gitignore file using Notepad++ and do:

Menu EditEOL ConversionWindows FormatSave.

Try using git status again and see if it works for you.

I have posted the answer to a similar question here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ozesh
  • 6,536
  • 1
  • 25
  • 23
1

My problem was that I wrote down files to ignore with quotes " " separation not with slash /.

This did not work and was ignored by git :

"db.sqlite3"
"tdd_venv/"

This worked just fine :

/db.sqlite3
/tdd_venv/

I also checked my file encoding in windows with Notepad++. Encoding was set to UTF-8.

JSalys
  • 159
  • 2
  • 3
1

I saw that a few of you already answered that but I don't know whether it was stated clearly.

In my case the problem appeared due to creating the .gitignore file with echo commmand which resulted in file encoding being changed.

In order to fix that I had to open the text file, click save as and change the encoding in the bottom tab to UTF-8.

When I used the touch command to create the file and edit its contents via text editor the problem did not occur.

1

Lot of answers here 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
  4. Go to File -> Save as
  5. Change the encoding to ANSI. (Some answers mistakenly say UTF-8 , but it doesn't work and wastes time needlessly)
  6. Again git rm -r --cached . and git add . (do not miss the dot)
  7. Now do Git status and verify what we wanted has happened
user3251882
  • 922
  • 1
  • 11
  • 21
1

Despite having tried the various suggestions in this post and others (e.g. Gitignore not working and gitignore not working - being ignored?) I couldn't get git to ignore the files/folders contained within my .gitignore.txt file.

I should stress that unlike many of the previous answers that address the problem whereby a file/directory that has already been previously committed is to be untracked, in my case, I encountered this problem prior to making my first commit (i.e. having only run git init followed by git status).

There were a number of other promising suggestions including:

  • text file encoding must be ANSI instead of UTF, e.g. Encoding -> ANSI in Notepad++
  • EOL conversion for Windows, e.g. Edit -> EOL Conversion -> Windows in Notepad++
  • no leading or trailing whitespaces for the items in .gitignore.txt
  • no leading "/" when defining a folder

but none were applicable or resolved my issue.

After playing around with different ways of creating a .gitignore.txt file I managed to figure out how to get it to work, although I can't say that I understand why. It would be interesting to know if anyone can offer an explanation.

For the benefit of those who may also find that the previous answers don't address their issue I'll describe what I tried, what didn't work and what did. Hopefully by doing so it may also shed light on the why. Note that I'm using Windows 10.

Attempt #1 (failed)

Creating the new txt file in Windows Explorer:

  1. (In the project folder) Right-click -> New -> Text document
  2. Changed file name to ".gitignore"
  3. Opened file in Notepad++, entered file/directory names to be ignored
  4. Encoding -> ANSI [Note 1], save changes

I ran git status but the files/folders I wanted ignored were shown as untracked. I checked the file properties in Windows Explorer. As expected the filename was ".gitignore" and the file type was "Text Document (.txt)".

Note 1:

Several contributors have stated that the encoding must be ANSI (rather than UTF). In Notepad++ I've found that regardless whether I do Encoding -> ANSI, or Encoding -> Convert to ANSI + save, close the file, and re-open it (in Notepad++), when I check the encoding it always reports UTF-8. This was also the case for the .gitignore.txt file that eventually worked.

Attempt #2 (failed)

Creating the new txt file in Notepad++:

  1. In Notepad++ created new file, entered items to ignore
  2. Saved as ".gitignore.txt", changed "Save as type" from "Normal text file" (default) to "All types", and un-ticked the box "Append extension" (ticked by default).

I ran git status and as before all files/directories were listed under untracked files. As before the filename was ".gitignore" and the file type was "Text Document (.txt)".

Attempt #3 (succeeded)

With the same file (.gitignore.txt) still open in Notepad++:

  1. Saved as ".gitignore" selected "Normal text file" for "Save as type", which automatically ticked the box "Append extension"

I noticed that the file created appeared to be name-less in Windows Explorer. I checked the file's properties: the file name field was blank and the file type was "Text Document (.gitignore)". After ticking "File name extensions" in Windows Explorer (View -> File name extensions) the "name-less" file shows the ".gitignore" extension.

I ran git status and this time the files/folders I wanted ignored were not listed as untracked files - success!

Opening the file in Notepad++ I noticed that the encoding was UTF-8, and the file name was identified as ".gitignore" (unlike in Windows Explorer).

So it appears there is subtlety in the way the txt file is created. Perhaps this is a peculiarity unique to Windows?

critor
  • 173
  • 2
  • 11
0

This might sound silly, but what solved my problem was realizing that .gitignore is working correctly, even if it says deleted: yourfile on git status.

I was going to ignore an error log file, to ignore any errors I caused, but save the ones caused by other developers in case they came in handy one day while debugging an odd problem. However, git does not let you "ignore" and also keep a file at the same time.

I wanted it to be "frozen in time", but git says it either gets tracked, or it is not allowed to be there at all.

Rock Lee
  • 9,146
  • 10
  • 55
  • 88
0

PhpStorm (and probably some other IDE users), in my case problem was that I created and added file outside the project, through the finder.

I deleted that one, and recreated the same one but in PhpStorm project, with right-click -> New -> File, and it worked right away.

Tahi Reu
  • 558
  • 1
  • 8
  • 19
0

My 30 cent trick:

  1. Pl clean your directory. git clean -f (I tried git rm -r --cached . but it did not work for me.)

Warning: git clean -f will remove untracked files, meaning they're gone for good since they aren't stored in the repository. Make sure you really want to remove all untracked files before doing this.

May be you can try git clean -xdf which removes ignored dirs and untracked files

  1. Now bring HEAD to the current position git reset HEAD --hard.
  2. check git status.

  3. Now create .gitignore which in my cases wanted to ignore a folder called data. So I wrote /data/ (initial / is for file/folder, later / is for folder only).

  4. Now do git add, commit, and push.
CKM
  • 1,911
  • 2
  • 23
  • 30
0

I experienced that .gitignore is ignored working with Flutter project in VS Code on Windows 10.

I tried many of the above suggestions. Nothing helped.

Final solution: just delete .git folder and start with a new repository. .git folder is located in the root of Flutter project and hidden by default.

Be sure to add folders and files to .gitignore before the first commit.

Yuriy N.
  • 4,936
  • 2
  • 38
  • 31