.gitignore 排除特定文件
.gitignore exclude specific file
我正在尝试使用 .gitignore
文件来排除位于 public/app/
文件夹中的 templates.js
。我的 .gitignore
文件如下所示:
/vendor
/node_modules
composer.phar
composer.lock
.DS_Store
templates.js
但是当我检查 Git Gui 时,templates.js
文件仍然显示在其中。如何使用 .gitignore
?
专门排除文件
一旦文件被 'added' 到 git 存储库一次,无论它是否在 .gitignore 文件中列出,它都将继续被跟踪。
我猜您之前添加了 template.js 文件,在这种情况下,它将继续跟踪更改,直到您将其从存储库中删除。从存储库中删除文件后,将通过 .gitignore 文件正确忽略它。
从 git 存储库(命令行)中删除文件而不删除它的方法是
git rm --cached FILENAME
然后,一旦您这样做并尝试提交 git 忽略中的文件,它就不会这样做。
请注意,如果您专门添加了 gitignore 中的文件,您手动添加的文件将覆盖 gitignore。
与名称 "ignore" 可能暗示的相反。 .gitignore
仅在您 git add
文件时查阅:换句话说,不会根据 .gitignore
.
排除已添加到存储库(的索引)的文件
首先你最好修改 .gitignore
这样文件就不再添加了。将以下行添加到 .gitignore
文件:
public/app/template.js
接下来您需要从存储库中排除该文件。可能您不想从您的文件系统中删除文件,这可以通过以下方式完成:
git rm --cached public/app/template.js
--cached
标志确保文件不会从您的文件系统中删除。 (如果不重要,你可以使用git rm public/app/template.js
,但是这会删除文件)。
背景
未主动使用 .gitignore
的原因是您有时可能想要覆盖 .gitignore
。比如你不想跟踪 *.log
个文件,你可以在 .gitignore
中指定 *.log
。但是如果你想跟踪一个特定的,你可以添加 git add -f some.log
。 -f
标志强制 git
添加文件。
如果您已经在跟踪文件(git add
和 git commit
),.gitignore
将无济于事。
我相信您想要的是停止跟踪文件,然后然后使用.gitignore
排除它。要停止跟踪文件但保留文件,请使用:
git rm --cached <file>
在这种情况下,您可以使用
停止跟踪templates.js
git rm --cached public/app/templates.js
正如其他人所解释的,.gitignore
只忽略要添加的内容。比如你经常使用git add .
,而你的.gitignore
里有templates.js
,那么git就不会加上。
我正在尝试使用 .gitignore
文件来排除位于 public/app/
文件夹中的 templates.js
。我的 .gitignore
文件如下所示:
/vendor
/node_modules
composer.phar
composer.lock
.DS_Store
templates.js
但是当我检查 Git Gui 时,templates.js
文件仍然显示在其中。如何使用 .gitignore
?
一旦文件被 'added' 到 git 存储库一次,无论它是否在 .gitignore 文件中列出,它都将继续被跟踪。
我猜您之前添加了 template.js 文件,在这种情况下,它将继续跟踪更改,直到您将其从存储库中删除。从存储库中删除文件后,将通过 .gitignore 文件正确忽略它。
从 git 存储库(命令行)中删除文件而不删除它的方法是
git rm --cached FILENAME
然后,一旦您这样做并尝试提交 git 忽略中的文件,它就不会这样做。
请注意,如果您专门添加了 gitignore 中的文件,您手动添加的文件将覆盖 gitignore。
与名称 "ignore" 可能暗示的相反。 .gitignore
仅在您 git add
文件时查阅:换句话说,不会根据 .gitignore
.
首先你最好修改 .gitignore
这样文件就不再添加了。将以下行添加到 .gitignore
文件:
public/app/template.js
接下来您需要从存储库中排除该文件。可能您不想从您的文件系统中删除文件,这可以通过以下方式完成:
git rm --cached public/app/template.js
--cached
标志确保文件不会从您的文件系统中删除。 (如果不重要,你可以使用git rm public/app/template.js
,但是这会删除文件)。
背景
未主动使用 .gitignore
的原因是您有时可能想要覆盖 .gitignore
。比如你不想跟踪 *.log
个文件,你可以在 .gitignore
中指定 *.log
。但是如果你想跟踪一个特定的,你可以添加 git add -f some.log
。 -f
标志强制 git
添加文件。
如果您已经在跟踪文件(git add
和 git commit
),.gitignore
将无济于事。
我相信您想要的是停止跟踪文件,然后然后使用.gitignore
排除它。要停止跟踪文件但保留文件,请使用:
git rm --cached <file>
在这种情况下,您可以使用
停止跟踪templates.js
git rm --cached public/app/templates.js
正如其他人所解释的,.gitignore
只忽略要添加的内容。比如你经常使用git add .
,而你的.gitignore
里有templates.js
,那么git就不会加上。