157

I am trying to use .gitignore file to exclude templates.js located in public/app/ folder. My .gitignore file looks like this:

/vendor
/node_modules
composer.phar
composer.lock
.DS_Store
templates.js

But when I check in the Git Gui, the templates.js file is still show in it. How can I specifically exclude a file using .gitignore?

user1995781
  • 19,085
  • 45
  • 135
  • 236
  • 3
    Does that mean the file is already added to the repository before? `.gitignore` only ignores content to be added. – Willem Van Onsem May 14 '15 at 01:33
  • 3
    @CommuSoft Yes, it has been added to repository before. So, how can I exclude it right now? Is my way of doing in `.gitignore` correct? – user1995781 May 14 '15 at 01:35

4 Answers4

246

In contrast to what the name "ignore" might suggest. .gitignore is only consulted when you git add files: in other words a file already added to the (index of the) repository will not be excluded based on the .gitignore.

First you better modify the .gitignore such that the file is no longer added. Add the following line to the .gitignore file:

public/app/template.js

Next you need to exclude the file from the repository. Probably you don't want to remove the file from your file system, this can be done with:

git rm --cached public/app/template.js

The --cached flag ensures the file will not be removed from your file system. (If not important, you can use git rm public/app/template.js, but this will remove the file).

Background

The reason .gitignore is not proactively used is because you sometimes might want to override the .gitignore. Say for instance you don't want to track *.log files, you can specify *.log in the .gitignore. But if there is a specific one you want to track you can add git add -f some.log. The -f flag forces git to add the file.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 9
    a life saver, had my credentials open to the public – mLstudent33 Sep 13 '20 at 03:54
  • 3
    @mLstudent33 I am no expert in git but just wanted to raise the concern that your credentials might still exist in previous versions in that repo. I may be wrong or you have already done something about it - just trying to help, with the little that I can! :) – DraxDomax Jun 28 '21 at 10:56
  • @DraxDomax: yes, you can do a "history rewrite": to remove the file over all commits: https://andrewlock.net/rewriting-git-history-simply-with-git-filter-repo/ – Willem Van Onsem Apr 21 '23 at 12:16
31

Once a file has been 'added' to the git repository once, it will continue to be tracked regardless of whether or not it is listed in the .gitignore file.

I'm guessing you added the template.js file previously, in which case it will continue to have changes tracked, until you remove it from the repository. Once the file has been removed from the repository, it will be properly ignored via the .gitignore file.

The way to remove a file from a git repository (command-line) without deleting it is

git rm --cached FILENAME

Then once you do that and try to commit with the file in your gitignore, it won't do it.

Note that if you specifically add a file that you have in your gitignore, your manual adding of the file will override the gitignore.

mprat
  • 2,451
  • 15
  • 33
BTownTKD
  • 7,911
  • 2
  • 31
  • 47
19

If you are already tracking a file (git add on it and git commit), .gitignore will not help.

I believe what you want is to stop tracking a file, and then use .gitignore to exclude it. To stop tracking a file but keep the file, use:

git rm --cached <file>

Ross
  • 2,130
  • 14
  • 25
  • didn't dv, but I think you perhaps should select your words better. *keep it in the repository* is a bit in contrast with *keep it on your local file system*. A repository does not *map* on a filesystem... – Willem Van Onsem May 14 '15 at 02:10
  • @CommuSoft Fixed to some satisfaction, I wasn't too careful because other people have answered it better. Thanks for the suggestion. – Ross May 14 '15 at 03:17
11

In this case you can stop tracking templates.js with

git rm --cached public/app/templates.js

As others have explained, .gitignore only ignores content to be added. For example, if you frequently use git add . and have templates.js in your .gitignore, then git will not add it.

taddy hoops
  • 593
  • 2
  • 16