Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
781 views
in Technique[技术] by (71.8m points)

git - .gitignore exclude specific file

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...