The .gitignore
file is a text file that tells Git which files or folders to ignore in a project.
.gitignore
文件是一个文本文件,告诉Git项目中要忽略的文件或文件夹。
A local .gitignore
file is usually placed in the root directory of a project. You can also create a global .gitignore
file and any entries in that file will be ignored in all of your Git repositories.
本地.gitignore
文件通常放置在项目的根目录中。 您还可以创建一个全局.gitignore
文件,该文件中的所有条目都会在您所有的Git存储库中被忽略。
To create a local .gitignore
file, create a text file and name it .gitignore
(remember to include the .
at the beginning). Then edit this file as needed. Each new line should list an additional file or folder that you want Git to ignore.
要创建本地.gitignore
文件,请创建一个文本文件并将其命名为.gitignore
(请记住在开头包含.
)。 然后根据需要编辑该文件。 每行都应列出您希望Git忽略的其他文件或文件夹。
The entries in this file can also follow a matching pattern.
该文件中的条目也可以遵循匹配模式。
*
is used as a wildcard match*
用作通配符匹配/
is used to ignore pathnames relative to the.gitignore
file/
用于忽略相对于.gitignore
文件的路径名#
is used to add comments to a.gitignore
file#
用于将注释添加到.gitignore
文件
This is an example of what the .gitignore
file could look like:
这是.gitignore
文件的外观示例:
# Ignore Mac system files
.DS_store
# Ignore node_modules folder
node_modules
# Ignore all text files
*.txt
# Ignore files related to API keys
.env
# Ignore SASS config files
.sass-cache
To add or change your global .gitignore file, run the following command:
要添加或更改全局.gitignore文件,请运行以下命令:
git config --global core.excludesfile ~/.gitignore_global
This will create the file ~/.gitignore_global
. Now you can edit that file the same way as a local .gitignore
file. All of your Git repositories will ignore the files and folders listed in the global .gitignore
file.
这将创建文件~/.gitignore_global
。 现在,您可以以与本地.gitignore
文件相同的方式编辑该文件。 您所有的Git存储库都将忽略全局.gitignore
文件中列出的文件和文件夹。
如何取消跟踪以前从新Gitignore提交的文件 (How to Untrack Files Previously Committed from New Gitignore)
To untrack a single file, ie stop tracking the file but not delete it from the system use:
要取消跟踪单个文件,即停止跟踪文件但不从系统中删除它,请使用:
git rm --cached filename
git rm --cached filename
To untrack every file in .gitignore
:
取消跟踪.gitignore
每个文件:
First commit any outstanding code changes, and then run:
首先提交所有未完成的代码更改,然后运行:
git rm -r --cached
git rm -r --cached
This removes any changed files from the index(staging area), then run:
这将从索引(临时区域)中删除所有更改的文件,然后运行:
git add .
git add .
Commit it:
提交:
git commit -m ".gitignore is now working"
git commit -m ".gitignore is now working"
To undo git rm --cached filename
, use git add filename
要撤消git rm --cached filename
,请使用git add filename
更多信息: (More Information:)
Git documentation: gitignore
Git文档: gitignore
Ignoring files: GitHub
忽略文件: GitHub
Useful
.gitignore
templates: GitHub有用的
.gitignore
模板: GitHub
翻译自: https://www.freecodecamp.org/news/gitignore-what-is-it-and-how-to-add-to-repo/