321

I added the following line to .gitignore:

sites/default/settings.php

but when I type git status it shows the file as unstaged file.

What's the problem? All other patterns work well.

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
Nick.h
  • 4,035
  • 5
  • 21
  • 22
  • 2
    possible duplicate of [Git: Remove a file from the repository without deleting it from the local filesystem](http://stackoverflow.com/questions/1143796/git-remove-a-file-from-the-repository-without-deleting-it-from-the-local-filesy) – Cascabel Feb 01 '12 at 20:31
  • I had the same problem, then I discovered I saved my .gitignore file on a different drive instead :face-palm: – Henry Aug 24 '17 at 11:48
  • It is a very bad idea to keep stuff in your repository and ignore the changes on it. – Yevgeniy Afanasyev Aug 29 '17 at 01:23
  • Make sure to have the repository initilized – Dan May 28 '21 at 18:31

15 Answers15

641

Make sure that your .gitignore is in the root of the working directory, and in that directory run git status and copy the path to the file from the status output and paste it into the .gitignore.

If that doesn’t work, then it’s likely that your file is already tracked by Git. You can confirm this through the output of git status. If the file is not listed in the “Untracked files” section, then it is already tracked by Git and it will ignore the rule from the .gitignore file.

The reason to ignore files in Git is so that they won't be added to the repository. If you previously added a file you want to be ignored, then it will be tracked by Git and the ignore rules matching it will be skipped. Git does this since the file is already part of the repository.

In order to actually ignore the file, you have to untrack it and remove it from the repository. You can do that by using git rm --cached sites/default/settings.php. This removes the file from the repository without physically deleting the file (that’s what the --cached does). After committing that change, the file will be removed from the repository, and ignoring it should work properly.

As pointed out by @display-name below, sometimes it's neccessary to do a git add . after the git rm --cached to properly rebuild the index.

JoelAZ
  • 3,875
  • 1
  • 11
  • 15
poke
  • 369,085
  • 72
  • 557
  • 602
  • What do you mean by 'root of the working directory'? The directory where the '.git' repository is found? – Jonathan Leffler Sep 30 '10 at 18:36
  • The working directory is the directory in which the `.git` directory is located and which itself is the repository's root. Like when you clone a repository to `/xy/` then `/xy/` is your working directory with `/xy/.git/` inside. – poke Sep 30 '10 at 18:39
  • 9
    Sometimes you'll also need to also do a `git add .` after `git rm --cached` to properly rebuild the index. – Display name Jan 05 '14 at 17:59
  • The idea to check it in the git status was brilliant. I kept checking it in the Git Staging in Eclipse and the path was incorrect. Thanks! – walla Apr 14 '16 at 09:04
  • 1
    I needed to add the -r (recursive) flag as I had nested folders checked in too. In my case I moved to the root of my git folder then ran the command git rm --cached -r .vs/ Where vs was the top-level folder that I wanted to remove from tracking. – RobbiewOnline Apr 23 '19 at 11:54
166

I run into this, it's an old question, but I want that file to be tracked but to not track it on certain working copies, to do that you can run

git update-index --assume-unchanged sites/default/settings.php
Mescalito
  • 1,968
  • 1
  • 11
  • 10
  • 8
    That's actually the answer I was looking for. All other answers assume that the file was added with git add, which is not always the case. With Acquia Cloud, the .gitignore file is supposed to ignore settings.php ( for instance ) but the file is included with the first commit. Untracking the file, simply deletes it from the repository , therefore deleting it from the live site... – PatrickS Nov 05 '13 at 09:31
  • Helped me too - thank you! Voted up. I suspect though it applies only to local git repo and wouldn't persist once pushed to remote and cloned by other devs? – Ivan Aug 31 '16 at 18:12
  • 1
    Yes, it's only local. – Mescalito Nov 14 '16 at 18:56
  • 4
    This answer lead me to the git function I personally needed, which was to avoid tracking my local changes to a settings file stored in the remote repository. The command I ended up using was `git update-index --skip-worktree filename` – coppereyecat Sep 09 '20 at 22:25
  • Would love to see how to revert this. `git update-index --no-assume-unchanged sites/default/settings.php` – wieczorek1990 Sep 07 '22 at 09:00
105

Please use this command

git rm -rf --cached .
git add .

Sometimes .gitignore files don't work even though they're correct. The reason Git ignores files is that they are not added to the repository. If you added a file that you want to ignore before, it will be tracked by Git, and any skipping matching rules will be skipped. Git does this because the file is already part of the repository.

ExpertWeblancer
  • 1,368
  • 1
  • 13
  • 28
  • 15
    Before you run this command, I recommend stashing all your changed . this caused a git mess in for me – Clifford Fajardo Oct 15 '21 at 20:17
  • or commit what you want to commit beforehand. this basically `git add`s all changes in your working directory – bunyaCloven Apr 12 '22 at 15:34
  • This worked for me. Since the files weren't added beforehand .gitignore was not excluding them. Thanks. – Joselo Jun 06 '22 at 16:45
  • Wow, did you `ctrl+c` and `ctrl+v` the accepted answer? The only paragraph of this answer is almost identical to that of the accepted one. – starriet May 04 '23 at 13:42
56

.gitignore will only ignore files that you haven't already added to your repository.

If you did a git add ., and the file got added to the index, .gitignore won't help you. You'll need to do git rm sites/default/settings.php to remove it, and then it will be ignored.

jonescb
  • 22,013
  • 7
  • 46
  • 42
  • thanks.but How do I remove the file only from index and not from working directory? – Nick.h Sep 30 '10 at 19:52
  • 1
    `git rm` should do that, but it may be asking you to use the -f option which would remove it from the working directory. I haven't figured this out other than to make a copy of the file, do `git rm -f` and then restore the copy. – jonescb Sep 30 '10 at 20:24
  • Same, man. Make a copy somewhere, remove it, commit removed state, add the files back and see how they are ignored now. – Yevgeniy Afanasyev Aug 29 '17 at 01:19
28

I had the same problem.
Files defined in .gitingore where listed as untracked files when running git status.

This was because the .gitignore file was saved in UTF-16LE encoding, and not in UTF8 encoding.

After changing the encoding of the .gitignore file to UTF8 it worked.

Floris Devreese
  • 3,127
  • 2
  • 21
  • 31
  • Any idea why the file encoding matters ? Maybe not reading path properly ? – Chargnn Dec 02 '19 at 20:09
  • Thank you so much. I read so many answers that said the problem was having committed the files at some point but I knew I hadn't committed these files. My problem was a .gitignore with a UTF-16LE encoding. It got that way because I created the file in powershell with `echo "" > .gitignore` – M Katz Sep 12 '22 at 07:15
7

What I did it to ignore the settings.php file successfully:

  1. git rm --cached sites/default/settings.php
  2. commit (up to here didn't work)
  3. manually deleted sites/default/settings.php (this did the trick)
  4. git add .
  5. commit (ignored successfully)

I think if there's the committed file on Git then ignore doesn't work as expected. Just delete the file and commit. Afterwards it'll ignore.

Alper Ebicoglu
  • 8,884
  • 1
  • 49
  • 55
7

There are instances e.g. Application Configuration files, which I want tracked in git (so .gitignore will not work), but that I need to change for local settings. I do not want git to manage these files or show them as modified. To do this I use skip-worktree:

git update-index --skip-worktree path/to/file

You can confirm files are skipped by listing files and checking for lines starting with S for skipped

git ls-files -v | grep ^S

If in the future you want to have git manage the file locally again simply run:

 git update-index --no-skip-worktree path/to/file

Mescalito above had a great answer, that led me down the right track but

git update-index --assume-unchanged file/to/ignore.php

Has a contract with git that in which : the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index.

However, I change the content of the files, so in my case --skip-worktree is the better option.

Toshiharu Nishina's website provided an excellent explanation of skip-worktree vs assume-unchanged: Ignore files already managed with Git locally

J.Leupp
  • 233
  • 3
  • 8
5

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 temp files mentioned in the .gitignore.
  3. Clear git cache; check whether .gitignore is UTF8; remove files -> commit -> move files back; skip 1 commit – nothing helped.

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

Solution: double-check multiple, hidden git clients and use only one git client at once, especially while clearing git cache.

Oleg Zarevennyi
  • 2,753
  • 1
  • 21
  • 21
3

One thing that I think has been missed in the excellent answers here is the existence of another .gitignore or multiple of them.

In a case, I had cloned a repo and could not figure out why a "ignored" file was being added again when running git add. It worked out that there was another .gitignore in a subfolder and that was overriding the one in the root folder.

/.gitignore
/some-folder/.gitignore
/some-folder/this-file-keeps-getting-staged

The

root /.gitignore

was ignoring this-file-keeps-getting-staged

The

/some-folder/.gitignore

did not have a specification to ignore "this-file-keeps-getting-staged" file.

And hence the issue.

One of the things is to simply search for multiple .gitignore files in the hierarchy. And figure out the rules as they apply.

Khanna111
  • 3,627
  • 1
  • 23
  • 25
  • 1
    Yeah, internal .gitignore files have higher priority than the .gitignore file at the repository root. THANK YOU!!!!! – GaidinD May 27 '22 at 14:29
  • 1
    Thanks for pointing this out and pointing out it's an edge-case. Probably not the OPs answer but good content and troubleshooting nonetheless. Upvoted. – JoelAZ Jun 16 '23 at 05:53
2

Make sure the .gitignore does not have a extension!! It can't be .gitignore.txt, in windows just name the file .gitignore. and it will work.

ZackOfAllTrades
  • 567
  • 5
  • 12
2

Just in case anyone in the future has the same problem that I did:

If you use the

*
!/**/
!*.*

trick to remove binary files with no extension, make sure that ALL other gitignore lines are BELOW. Git will read from .gitignore from the top, so even though I had 'test.go' in my gitignore, it was first in the file, and became 'unignored' after

!*.*
2

I tried most commands above on VS Code terminal and I got errors like:

fatal: pathspec '[dir]/[file]' did not match any files

I opened the project on GitHub Desktop and ignored from there and it worked.

1

I just tried this with git 1.7.3.1, and given a structure like:

repo/.git/
repo/.gitignore
repo/sites/default/settings.php

where repo thus is the "root" mentioned above (I would call it the root of your working tree), and .gitignore contains only sites/default/settings.php, the ignore works for me (and it does not matter whether .gitignore is added to the repo or not). Does this match your repo layout? If not, what differs?

Johan
  • 636
  • 5
  • 11
1

One tricky problem is that If you do something like echo node_modules >> .gitignore, it won't work. The windows terminal saves the file in UCS-2 LE BOM and git doesn't seem to accept that.

You can open the file with Notepad and save it with UTF-8 encoding

notepad save utf-8 encoding

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
-1

I had the same problem. On Win10 I used the Geany editor and it uses UNIX line-ends. Now I created my .gitignore using notepad.exe and all is well.

Radim Cernej
  • 875
  • 1
  • 10
  • 21