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?

  • 2
    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 1:33
  • 1
    @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 1:35
up vote 53 down vote accepted

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.

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.

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>

  • 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 2: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 3:17

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.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.