git 忽略和.gitignore
git 忽略
与他人共享代码时,您通常不想共享项目的文件或部分内容。
示例
- 日志文件
- 临时文件
- 隐藏文件
- 个人档案
- ETC。
Git 可以使用以下命令指定 Git 应忽略项目的哪些文件或部分:.gitignore
文件。
Git 不会跟踪指定的文件和文件夹.gitignore
。但是,那.gitignore
文件本身是由 Git 跟踪。
创建.gitignore
创建一个.gitignore
文件,转到本地 Git 的根目录并创建它:
示例
touch .gitignore
现在使用文本编辑器打开该文件。
我们只需添加两个简单的规则:
- 忽略任何带有以下内容的文件
.log
扩大 - 忽略任何名为的目录中的所有内容
temp
示例
# ignore ALL .log files
*.log
# ignore ALL files in ANY directory named temp
temp/
现在所有.log
文件和任何东西temp
Git 将忽略文件夹。
笔记:在本例中,我们使用单个.gitignore
这适用于整个存储库。
也可以有额外的.gitignore
子目录中的文件。这些仅适用于该目录中的文件或文件夹。
.gitignore 的规则
以下是匹配模式的一般规则.gitignore
文件:
Pattern | Explanation/Matches | Examples |
---|---|---|
Blank lines are ignored | ||
# text comment | Lines starting with # are ignored | |
name | All name files, name folders, and files and folders in any name folder | /name.log /name/file.txt /lib/name.log |
name/ | Ending with / specifies the pattern is for a folder. Matches all files and folders in any name folder | /name/file.txt /name/log/name.log no match: /name.log |
name.file | All files with the name.file | /name.file /lib/name.file |
/name.file | Starting with / specifies the pattern matches only files in the root folder | /name.file no match: /lib/name.file |
lib/name.file | Patterns specifiing files in specific folders are always realative to root (even if you do not start with / ) | /lib/name.file no match: name.file /test/lib/name.file |
**/lib/name.file | Starting with ** before / specifies that it matches any folder in the repository. Not just on root. | /lib/name.file /test/lib/name.file |
**/name | All name folders, and files and folders in any name folder | /name/log.file /lib/name/log.file /name/lib/log.file |
/lib/**/name | All name folders, and files and folders in any name folder within the lib folder. | /lib/name/log.file /lib/test/name/log.file /lib/test/ver1/name/log.file no match: /name/log.file |
*.file | All files withe .file extention | /name.file /lib/name.file |
*name/ | All folders ending with name | /lastname/log.file /firstname/log.file |
name?.file | ? matches a single non-specific character | /names.file /name1.file no match: /names1.file |
name[a-z].file | [range] matches a single character in the specified range (in this case a character in the range of a-z, and also be numberic.) | /names.file /nameb.file no match: /name1.file |
name[abc].file | [set] matches a single character in the specified set of characters (in this case either a, b, or c) | /namea.file /nameb.file no match: /names.file |
name[!abc].file | [!set] matches a single character, except the ones spesified in the set of characters (in this case a, b, or c) | /names.file /namex.file no match: /namesb.file |
*.file | All files withe .file extention | /name.file /lib/name.file |
name/ !name/secret.log |
! specifies a negation or exception. Matches all files and folders in any name folder, except name/secret.log | /name/file.txt /name/log/name.log no match: /name/secret.log |
*.file !name.file |
! specifies a negation or exception. All files withe .file extention, except name.file | /log.file /lastname.file no match: /name.file |
*.file !name/*.file junk.* |
Adding new patterns after a negation will re-ignore a previous negated file All files withe .file extention, except the ones in name folder. Unless the file name is junk |
/log.file /name/log.file no match: /name/junk.file |
本地和个人 Git 忽略规则
也可以忽略文件或文件夹但不将其显示在分布式中.gitignore
文件。
这些类型的忽略在 .git/info/exclude
文件。它的工作方式与.gitignore
但不会向其他任何人展示。