ChatGPT解决这个技术问题 Extra ChatGPT

.gitignore is ignored by Git

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?

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 this was exactly the problem on my machine (Windows). I has used echo "file" > .gitignore in PowerShell, the file had a UCS-2 encoding!
git rm --cached debug.log nbproject/
Why the first comment here is not an answer is beyond me
@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!

B
Braian Coronel

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"

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.
on Windows: git rm . -r --cached and git add .
Be aware to commit all your changes before, otherwise you will lose control on all the changed files!!
The first 3 commenters seem to have missed the dot in the end. This means every file. "git -rm -r --cached ." <-- notice the dot.
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"
i
ifeegoo

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


Perhaps could you edit @AlinHuruba 's answer to add your step 3 ?
@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.
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.
Hope this helps you!
I'm happy that this can help you!
P
Peter Mortensen

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!


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.
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.
My problem was similar - my .gitignore was using UTF8 with a BOM. Just saved it as UTF8 without a BOM and it magically started working.
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.
Had the same problem as @Laura in powershell - the file is by default saved as UTF16.
P
Peter Mortensen

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.


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 :)
@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: stackoverflow.com/questions/37279654/…
I got a "fatal: pathspec 'debug.log' did not match any files"
@HAßdøµ In plain English, it deletes any history of changes related to them, and also will not track their change in any future commit. This plain-speak explanation is what I was looking for! Nandri 🙏
P
Peter Mortensen

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

A trailing space was my problem, which is even even trickier to find.
also had a trailing whitespace problem.
Yes, the same here. I thought I could write comments after the file pattern: *.txt # Tex files. But git understands: "Ignore the files with the extension txt "
My word, took me 2 days to find this 15-upvoted answer. This deserves more.
You just resolved a problem I had for months... Thank you !
C
Community

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:

Verify status: PS> git status Create a function to Get-FileEncoding Test .gitignore's encoding: PS> Get-FileEncoding .gitignore Change the encoding to ASCII: PS> Set-Content .gitignore -Encoding Ascii -Value (Get-Content .gitignore) Confirm: PS> git status


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.
You can try by skipping Step 2 & 3.
P
Peter Mortensen

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.


this one is not working for me but ifeegoo's answer does
Lovely jubbley Rodders.
P
Peter Mortensen

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.


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.
P
Peter Mortensen

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.


Thank god... finally a solution which works for me.
~/.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.
A workaround if you didn't create .gitignore first: rename the tracked file. .gitignore will be applied as if it were new.
P
Peter Mortensen

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

P
Peter Mortensen

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.


D
Daniel Fisher lennybacon

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.


P
Peter Mortensen

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

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.
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!
V
Vlad L.

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!


P
Peter Mortensen

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.


P
Peter Mortensen

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.

That looks like a UTF-16 byte order mark in a UTF-8-encoded file. en.wikipedia.org/wiki/Byte_order_mark
P
Peter Mortensen

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.


J
Jesse

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>

P
Peter Mortensen

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


P
Peter Mortensen

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


P
Peter Mortensen

Another possible reason – a 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:

You commit something. Next, commit: it ignores .gitignore settings. Commit includes lots of temporary files mentioned in the .gitignore. 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.


P
Peter Mortensen

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.


J
James

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

P
Peter Mortensen

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.


P
Peter Mortensen

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

this deserves more vote. adding !.gitignore solves my problem.
P
Peter Mortensen

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.


U
U.A

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

touch .gitignore


K
Kari

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


P
Peter Mortensen

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"

P
Peter Mortensen

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

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

Menu Edit → EOL Conversion → Windows Format → Save.

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

I have posted the answer to a similar question here.