457

See the image below. My .gitignore file should be ignoring all files in src/dist, but isn't.

enter image description here

Richard
  • 14,798
  • 21
  • 70
  • 103
  • 3
    If you already added those files and `git` is tracking them, the `.gitignore` file has no effect because it is meant for untracked files. See a good solution here: https://stackoverflow.com/a/23673910/2430526 – SRG Apr 29 '22 at 01:05

12 Answers12

852

.gitignore only ignores files that are not part of the repository yet. If you already git added some files, their changes will still be tracked. To remove those files from your repository (but not from your file system) use git rm --cached on them.

Elmar Peise
  • 14,014
  • 3
  • 21
  • 40
  • 88
    git rm --cached file_name.ext wroks fine for me to update gitignore for one file. thanks. – sybozz Nov 21 '19 at 06:31
  • 11
    I did this, but GitHub still wants to track and add them. – Netside Jul 01 '20 at 02:36
  • 16
    git rm -r --cached folder to recursively remove the cache on a folder – Pablo Pazos Sep 26 '20 at 15:42
  • Ignoring the files didn't work for me but I ignored the dist folder like Pablo mentioned and it worked beautifully! – Mahsa Oct 30 '20 at 16:36
  • @sybozz and with the beauty of bash pattern globbing (using the * character) it works for lots of files too. – Lorenzo Nov 17 '20 at 04:07
  • i am using "git rm --cached" and it is returning information - usage: git rm [] [--] ... -n, --dry-run dry run -q, --quiet do not list removed files --cached only remove from the index -f, --force override the up-to-date check -r allow recursive removal --ignore-unmatch exit with a zero status even if nothing matched – Kamlesh Dec 04 '20 at 11:37
  • But, this shows that the file node is deleted and when I pushed my changes after executing this command, the file in the server got deleted :( . Did anyone else face this issue ? – Cjo Jul 20 '21 at 10:11
  • This did not work for me, but I noticed that my file had already been staged. Removing it via `git restore --staged ` did the trick! – markand Jul 23 '21 at 19:19
  • In case of error on applying command, use --force, -f before the file name that resolves the case!!! – WMS Aug 09 '21 at 21:12
  • User `.` for current working directory or specify another directory for successful execution – Muhammad Zubair Dec 27 '21 at 17:08
  • finally some one explains this like a human – Yasser CHENIK Jul 13 '23 at 16:45
386

The .gitignore file ensures that files not tracked by Git remain untracked.

Just adding folders/files to a .gitignore file will not untrack them -- they will remain tracked by Git.

To untrack files, it is necessary to remove from the repository the tracked files listed in .gitignore file. Then re-add them and commit your changes.

The easiest, most thorough way to do this is to remove and cache all files in the repository, then add them all back. All folders/files listed in .gitignore file will not be tracked. From the top folder in the repository run the following commands:

git rm -r --cached .
git add .

Then commit your changes:

git commit -m "Untrack files in .gitignore"

Please note that any previous commits with the unwanted files will remain in the commit history. When pushing to GitHub be aware of a commit history that might contain .env or client_secret.json files.

Best practice is to create a .gitignore file and populate it with the folders/files you do not want tracked when starting a project. However, often it is necessary to add to the .gitignore file after realising that unwanted files are being tracked and stored.

Gitau Harrison
  • 3,179
  • 1
  • 19
  • 23
MarckK
  • 4,061
  • 1
  • 10
  • 4
  • 2
    thank you for the answer. This really helped. Don't forget to push the repo up to git either – Chris Reed Apr 24 '20 at 13:15
  • 1
    After using `git rm -r --cached .` rolling back all changes also works to re-add all of the files that should not be excluded with _.gitignore_. – AdamHurwitz Aug 05 '20 at 17:51
  • 4
    This should be marked as the correct answer – iskandarblue Apr 18 '21 at 19:37
  • 1
    This is the more complete answer – Rohit Gupta Oct 01 '21 at 14:02
  • 2
    This should be marked as the correct answer – Taha Ali Dec 27 '21 at 12:08
  • Doing this lists every file in our repo as modified and wants to commit everything :( Even though none of the files have changed, git is considering them modified. Why is this? – NightCabbage Apr 11 '22 at 22:45
  • This should be the accepted answer. I needed extra explanation of how it's necessary to run a git commit -m "..." after git rm -r --cached – Chris Chalmers Mar 22 '23 at 19:11
  • As a side note, in GitHub Desktop, when you have a project in a directory that you want to upload for the first time, and a .gitignore file it with a proper spec of what you want to exclude, DO NOT select the gitignore option in GitHub Desktop, as it will overwrite your own. – RolfBly May 07 '23 at 18:35
44

You can use this,

git rm -r --cached ./node_modules

if you want to ignore node_modules, for example

dtwk2
  • 75
  • 6
sam r
  • 441
  • 4
  • 2
30

gitignore ignores only untracked files. Your files are marked as modified - meaning they were committed in the past, and git now tracks them.

To ignore them, you first need to delete them, git rm them, commit and then ignore them.

pltc
  • 5,836
  • 1
  • 13
  • 31
Igal S.
  • 13,146
  • 5
  • 30
  • 48
21

first check the .gitignore encoding.
make sure the encoding id utf-8.
then untracked unwanted file by using git rm --cached filename now your problem fixed

  • 2
    The file reencoding did it for me. – cesarv Sep 20 '22 at 21:53
  • +1 ... Created my .gitignore via 'touch' in MINGW64 shell and didn't notice that the encoding was NOT utf-8 (had .gitignore already open in Notepad++ so checking and changing encoding was easy). Wish I had googled earlier. – Bruce Loth Oct 04 '22 at 20:37
9

Look at this : .gitignore is not working And particularly the remark from ADTC:

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

Philippe Raemy
  • 393
  • 5
  • 10
4

If you are using VS Code:

Check the .gitignore file's Encoding Type. Change it to UTF-8 if it is not. Select the Reopen with Encoding option to change the file encoding.

This worked for me!

enter image description here

PKPrabu
  • 409
  • 4
  • 18
0

Its better to create .gitignore file in starting and mentioning the files we want to be ignored. If you want to ignore some files then execute git rm -r --cached

Shivani
  • 33
  • 6
0

I was facing the same issue and then I realized that I had not created the .gitignore file properly.

In my case for some reason I created a .gitignore.txt file. Git will still track the files in the .gitignore file even after you remove the '.txt'. extension and save it.

Try deleting your old file and creating a new .gitignore file by 'touch .gitignore' in terminal, which solved it for me.

Dharman
  • 30,962
  • 25
  • 85
  • 135
nospec
  • 41
  • 5
0

For some of the files in my project I needed to use git filter-branch which is better explained in this answare.

0

If all of the above solutions didn't work, please try this.

The target files which you want to ignore should not be in staging area. Please double check it.

XnivaXhzne
  • 41
  • 4
-1

First delete the index.lock file from your git repo

rm -f .git/index.lock

and then add .gitignore

git add .gitignore
KayV
  • 12,987
  • 11
  • 98
  • 148