问题描述
.gitignore中有3件事:
I have 3 things in my .gitignore:
node_modules/
credentials.js
bitbucket-pipelines.yml
问题在于,每当我对credentials.js
进行更改时,我仍然可以在打开Sourcetree时提交该文件.
The issue is that, whenever I make changes to credentials.js
, I am still able to commit that file when I opened up my Sourcetree.
我该如何解决?
推荐答案
与名称"ignore"可能暗示的相反.仅当您使用git add
文件时才引用.gitignore
:换句话说,基于.gitignore
,不会排除已经添加到存储库(的索引)的文件.
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
.
首先,最好修改.gitignore,以便不再添加文件.将以下行添加到.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/credentials.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/credentials.js
--cached
标志可确保不会从文件系统中删除文件. (如果不重要,则可以使用git rm public/app/credentials.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/credentials.js, but this will remove the file).
这篇关于.gitignore不能按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!