Gitignore解释:什么是Gitignore,以及如何将其添加到您的仓库中

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:)

翻译自: https://www.freecodecamp.org/news/gitignore-what-is-it-and-how-to-add-to-repo/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值