250

In the root of my project I have a foo folder. Inside the foo folder I have a bar folder. I would like to ignore all changes to all files inside my bar folder. I have this in my gitignore:

/foo/bar

The folder is checked: it exists and it has the files to be ignored. gitignore is committed. However, I have a file where I make a moification and is inside my bar folder. When I type

git status

inside my git bash I see the file which should have been ignored. What could be the reason and how can I successfully ignore all files inside my bar folder?

Note, that the files were previously ignored with the same line, but I had to temporarily remove that line to commit something on the server. After the commit, I put back the line into the gitignore. This was a while ago, but now I have observed that the files will be in git status. I would expect to be able to modify the ignored files without they appearing in the git status.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • 2
    In the future, you don't need to modify your ignores to add something ignored. `git add -f ignored-file` will add a file even if it's in your ignores. – ChrisGPT was on strike Jun 25 '14 at 13:38
  • What does `git status foo/bar/file-that-should-be-ignored` give? – ChrisGPT was on strike Jun 25 '14 at 13:42
  • The file which should be ignored if any change occured. I am obviously missing something, but according to my knowledge the list of git status should be empty in that case provided that /foo/bar is present in .gitignore – Lajos Arpad Jun 25 '14 at 14:15
  • 7
    It sounds like that file is actually being tracked by Git. It must have been added to the repository at some point. You can see its history with `git log foo/bar/file-that-should-be-ignored`. If it shouldn't be part of the repository you should remove it with `git rm --cached foo/bar/file-that-should-be-ignored` and then commit as suggested by Reck below (though I would just do it on the file, not the whole folder). This will (a) remove the file from Git, (b) *keep the file* in your local copy, and (c) cause the file to be deleted when others `fetch` or `pull` the new commit. – ChrisGPT was on strike Jun 25 '14 at 14:34
  • I want to remove all files in the folder from git, but of course, I want to keep them. The folder contains local settings for development environments, therefore the content of the whole folder should be ignored by git – Lajos Arpad Jun 25 '14 at 14:40
  • Then `git rm --cached` is likely correct. Just bear in mind that every other copy will lose those files when they integrate the commit; you'll need to be careful when merging it in. – ChrisGPT was on strike Jun 25 '14 at 14:41
  • Yes, you are absolutely right, it must be done with care. But if local settings are not proper, the project will not work on the local machine, so we will know whenever careless copy occured. – Lajos Arpad Jun 25 '14 at 14:44
  • duplicated: http://stackoverflow.com/a/1139797/274502 – cregox Nov 15 '15 at 08:10
  • 1
    Possible duplicate of [Ignore files that have already been committed to a Git repository](http://stackoverflow.com/questions/1139762/ignore-files-that-have-already-been-committed-to-a-git-repository) – cregox Nov 15 '15 at 08:11
  • 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) – Giulio Caccin Nov 19 '18 at 16:27

7 Answers7

641

I'm guessing this folder has been checked into git before?

Run git rm -r --cached <folder> and check again.

Black
  • 18,150
  • 39
  • 158
  • 271
Reck
  • 7,966
  • 2
  • 20
  • 24
  • 11
    This will completely remove the folder from the (current version of the) repo. OP wants the folder to be there, but new changes should not be tracked – Gareth Jun 25 '14 at 13:39
  • Unfortunately I have to remove the accept. This solution removed the remote versions of the files when pushed. I want to set up gitignore to ignore all files of a folder, not to remove it from remote. Is that possible? I have reverted the solution. – Lajos Arpad Jun 25 '14 at 15:42
  • 1
    I want the remote server to keep its version of the files – Lajos Arpad Jun 25 '14 at 15:43
  • 25
    `git update-index --assume-unchanged ` perhaps? – Reck Jun 25 '14 at 16:15
  • http://git-scm.com/docs/git-update-index - it stops git from checking if there are any modifications made in that path. Note that it will only do this on YOUR machine. So if you have colleagues who need this ignored as well, this is not the right approach. – Reck Jun 26 '14 at 10:32
  • Well then, it seems this is not the right approach. If the path is in gitignore, is it in a cache which will eventually be emptied? – Lajos Arpad Jun 28 '14 at 19:05
  • 3
    @Lajos shouldn't you just have fixed his bug instead of adding this confusing "edit" comment there? – cregox Nov 15 '15 at 08:09
  • 3
    @Cawas, as this site is about knowledge sharing, the answer is: no. It is misleading to accept an answer which contains a bug. That -r is not optional. – Lajos Arpad Nov 15 '15 at 11:22
  • @LajosArpad that's what I mean. Fix the bug in the edition. – cregox Nov 15 '15 at 11:27
  • This answer, which is older than a year, edited more than a year ago was upvoted as it is. So it was helpful for users in its current form. – Lajos Arpad Nov 15 '15 at 11:28
  • I see this answer has 135 -1=134 votes but it does not answer the OP question. I am in the same dilemma. I have added .vs/ in my .git/.gitignore file but the .vs folder is always shown in git status command which is annoying. – learner Oct 02 '18 at 23:14
  • I had been banging my head against the wall for two days because of this lol. How did i forget about caching. I though it was intellij :( – Hemil Mar 14 '20 at 05:30
  • This worked for me. I would like to add that I needed to restart VS code to see the folder marked in gray in the explorer. – Pablo Oct 03 '20 at 19:45
  • This didn't work. It doesn't seem like my folder has been already pushed. I get the following error "fatal: pathspec 'unpackaged' did not match any files". – Bahman.A Mar 08 '21 at 20:04
  • I'm getting `fatal: : '' is outside repository` – Ben Alan Apr 13 '22 at 22:25
  • @BenAlan Try to enter full path to that folder. I have had the same situation like you. – Kamil May 12 '22 at 10:13
  • Running the command told me, that my files were already staged: so thats why i couldn't ignore the folder. After unstaging the files, the folder was ignored. – hiroorih Mar 30 '23 at 12:16
113

For me, the accepted answer was part of the solution, not the entire solution. Maybe, the other steps that I'm about to post were obvious, but I missed them first. Here's the steps I took to ensure my .gitignore file ignored the folder I wanted it to ignore:

  1. Commit any changes that you need to fix/change.
  2. Run this command: git rm -r --cached . (which removes everything from the git index in order to refresh your git repository)
  3. Then run this command: git add . (to add everything back to the repo)
  4. Finally, commit these changes using git commit -m ".gitignore Fixed"

You can find the link to the article from where I found the solution here.

sikanderBabwani
  • 1,289
  • 1
  • 9
  • 10
  • 3
    This is not a viable solution based on the original question. @lagos-arpad specifically asked about a subdirectory and not the entire project. Your solution removes everything from the root level of the git repository and this is not a recommended approach to deal with this problem. – justinhartman Jan 03 '18 at 21:29
  • Even if it's only a subdirectory , this solution worked for me as I was dealing with only one file. Can you please elaborate why it's not recommended? – sikanderBabwani Jan 05 '18 at 15:09
  • 1
    What you were dealing with is immaterial. This is not the answer to the question and is potentially dangerous as the other answer has pointed out. This removes stuff on the remote repo which you don't necessarily want. – RichieHH Sep 04 '18 at 10:55
  • this worked exactly as i needed it to work. weirdly the latest commit showed the previous commits and not ".gitignore Fixed"! But again, my issue was solved – Nithin Sai Sep 28 '20 at 17:13
  • 1
    @justinhartman , you can specify your folder name by replacing . with – Kritish Bhattarai Nov 21 '20 at 03:00
22

This solved it

I had done echo node_modules >> .gitignore and it didn't work.

the windows terminal from saves the file in UCS-2 LE BOM and git doesn't seem to accept that.

I opened the file with Notepad and saved 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
  • 2
    I had the same issue when redirecting Powershell's output to gitignore. E.g. `Get-ChildItem . -Filter *.csv -recurse >> .gitignore` did not work until I changed the encoding to UTF-8. And yes, I know ignoring all csv files can be achieved directly via .gitignore: This was just meant as an example ;) – and0r Sep 29 '21 at 15:40
17

I was having this issue and I realized git was actually ignoring the files/folders correctly, but my code editor (Visual Studio Code) was just buggy and not "greying them out" properly in the UI sidebar. I restarted VSCode and they were greyed out as expected.

remjx
  • 4,104
  • 3
  • 34
  • 32
11

As an addition to the accepted answer

git rm -r --cached /foo/bar/
git status

When i do that, the terminal shows a bunch of rm for files in that directory. So to prevent another commit that might unnecessarily affect remote, I i did:

git reset HEAD *
git status

After that, it said nothing to commit and when i modify files inside /foo/bar/ and do a git status i still get nothing to commit.

jtlindsey
  • 4,346
  • 4
  • 45
  • 73
6

I created the .gitignore in powershell using echo "build" > .gitignore. This created a file with UTF-16LE encoding. Git did not recognize the contents of the file and continued to show the build folder as untracked. I deleted the file and recreated it in the geany text editor (UTF-8) and git now ignores the build folder. A short search re-inforces that that git doesn't like UTF-16.

Dinsdale
  • 581
  • 7
  • 12
  • I figured this out finally, before I saw your answer. Then I scrolled through the rest of the questions expecting to see someone commenting / answering on this scenario :P – joshmcode Mar 25 '22 at 22:31
1

Most of the time it's an encoding issue. So you may delete the .gitignore file and create it again.

EDIT:

Proof:

  1. Create a folder called test-gitignore
  2. Go to that folder and initialize a git repo: git init
  3. Create a .env file and add anything there
  4. Now you want to ignore that .env file, so what you should do is create a .gitignore file in the same directory having a content: .env. But the way you create that .gitignore file is important. If you create it using powershell's echo: "echo >> .gitignore", your .env will not be ignored, because git expects a utf-8 encoded .gitignore file. In PowerShell, the default encoding used by the echo command for writing text to a file is Unicode (UTF-16LE). This means that when you use the echo command to append text to a file using >>, the text will be written in UTF-16LE encoding.

This is why it's important to create the file using vscode, or any tool ensure a utf-8 encoding.

Alan Deep
  • 2,037
  • 1
  • 14
  • 22
  • No, this issue was caused by the fact that some files inside the folder were already versioned. – Lajos Arpad Nov 10 '22 at 09:36
  • That's another story. But if you haven't versioned any of these files inside the folder, then that's most probably then answer (I've been there). – Alan Deep Nov 10 '22 at 14:18
  • That's an interesting point. In the context of this question, it was definitely an already versioned file in that folder. But I'm still happy to upvote your answer if you provide reproduction steps for the phenomenon you have described above. – Lajos Arpad Nov 11 '22 at 09:48