gitignore

前端之家收集整理的这篇文章主要介绍了gitignore前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Git Igitignore

Summary

本文摘自atlassian并添自己的理解,如果有理解错误或者偏差的地方欢迎指出
Git sees every file in your working copy(工作副本,工作区)as one of three things:

  1. tracked - a file which has been previously staged or committed
  2. untracked - a file which has not been staged or committed; or
  3. ignored - a file which Git has been explicitly told to ignore.

Ignored files are usually build artifacts and machine generated files
that can be derived from your repository source or should otherwise not be committed.
Some common examples are:

  • dependency caches(缓存依赖文件),such as the contents of /node_modules or /packages
  • compiled code(编译中间代码),such as .o,.pyc,and .class files
  • build output directories,such as /bin,/out,or /target
  • files generated at runtime(运行时产生的代码),such as .log,.lock,or .tmp
  • hidden system files,such as .DS_Store or Thumbs.db
  • personal IDE config files,such as .idea/workspace.xml

Ignored files are tracked in a special file named .gitignore that is checked(检测) in at the root of your repository.
There is no explicit(明显的,显示的) git ignore command: instead the .gitignore file must be edited and committed by hand when you have new files that you wish to ignore. .gitignore files contain patterns(模式) that are matched against file names in your repository to determine whether or not they should be ignored
注意: .gitignore 不要写错不然不会起作用,一般习惯把一个工程有一个.gitignore文件并且放在厂库的根目录.

Git ignore patterns

.gitignore uses (globbing patterns :通配符模式) to match against file names. You can construct your patterns using varIoUs symbols:

Pattern Example matches Explanation* **/logs logs/debug.log
logs/monday/foo.bar
build/logs/debug.log **/logs/debug.log logs/debug.log
build/logs/debug.log
but not
logs/build/debug.log *.log debug.log
foo.log
.log
logs/debug.log *.log
!important.log debug.log
trace.log
but not
important.log
logs/important.log also matches a negating pattern defined later in the file,it will not be ignored. *.log
!important/*.log
trace.* debug.log
important/trace.log
but not
important/debug.log IoUsly negated files.[在否定模式之后(!)定义的模式将重新忽略以前被否定的任何文件 eg:important/trace.log] /debug.log debug.log
but not
logs/debug.log debug.log debug.log
logs/debug.log debug?.log debug0.log
debugg.log
but not
debug10.log debug[0-9].log debug0.log
debug1.log
but not
debug10.log debug[01].log debug0.log
debug1.log
but not
debug2.log
debug01.log debug[!01].log debug2.log
but not
debug0.log
debug1.log
debug01.log debug[a-z].log debuga.log
debugb.log
but not
debug1.log logs logs
logs/debug.log
logs/latest/foo.bar
build/logs
build/logs/debug.log logs are ignored logs/debug.log
logs/latest/foo.bar
build/logs/foo.bar
build/logs/latest/debug.log logs/
!logs/important.log logs/debug.log
logs/important.log logs/important.log be negated in the example on the left

Nope! Due to a performance-related quirk in Git,you can not negate a file that is ignored due to a pattern matching a directory(等一下!logs/important.log不应该在左边的示例中被否定。 不!考虑到git中性能相关的原因,您不能否定由模式匹配的目录而被忽略的文件,这里指logs/important.log ) logs/**/debug.log logs/debug.log
logs/monday/debug.log
logs/monday/pm/debug.log logs/*day/debug.log logs/monday/debug.log
logs/tuesday/debug.log
but not
logs/latest/debug.log logs/debug.log logs/debug.log
but not
debug.log
build/logs/debug.log
** these explanations assume your .gitignore file is in the top level directory of your repository,as is the convention. If your repository has multiple .gitignore files,simply mentally replace "repository root" with "directory containing the .gitignore file" (and consider unifying them,for the sanity of your team). 这些解释假定.gitignore文件位于仓库的顶层目录中,如果仓库中有多个.gitignore文件,那么只需将仓库根目录包含.gitignore文件即可,这也是惯例。(并考虑将它们统一起来,以使您的团队保持清醒)。

注意:正规表达式是和通配符是完全不一样的东西,通配符(wildcard)代表的是bash匹配文件路径一个功能;正规表达式是一种字符串处理的表示方式,别搞混了.

In addition to these characters,you can use # to include comments in your .gitignore file:

# ignore all logs
*.log

You can use to escape .gitignore pattern characters if you have files or directories containing them(可以利用对包含通配符的字符进行转义):

# ignore the file literally named foo[01].txt
foo\[01\].txt

Shared .gitignore files in your repository

Git ignore rules are usually defined in a .gitignore file at the root of your repository.
However,you can choose to define multiple .gitignore files in different directories in your repository.
Each pattern in a particular .gitignore file is tested relative to the directory containing that file.
However the convention,and simplest approach,is to define a single .gitignore file in the root.
As your .gitignore file is checked in,it is versioned like any other file in your repository and shared with your teammates when you push.
Typically you should only include patterns in .gitignore that will benefit other users of the repository.

Personal Git ignore rules

You can also define personal ignore patterns for a particular repository in a special file at .git/info/exclude.
These are not versioned,and not distributed with your repository(并不会加入到版本控制中),so it's an appropriate place to include patterns that will likely only benefit you.
For example if you have a custom logging setup,or special development tools that produce files in your repository's working directory,you could consider adding them to .git/info/exclude to prevent them from being accidentally committed to your repository.

Global Git ignore rules

In addition,you can define global Git ignore patterns for all repositories on your local system by setting the Git core.excludesFile property.
You'll have to create this file yourself. If you're unsure where to put your global .gitignore file,your home directory isn't a bad choice (and makes it easy to find later). Once you've created the file,you'll need to configure its location with git config:

$ touch ~/.gitignore
$ git config --global core.excludesFile ~/.gitignore  //这里是吧home目录的.gitignore 作为全局忽略文件

You should be careful what patterns you choose to globally ignore,as different file types are relevant for different projects.Special operating system files (e.g. .DS_Store and thumbs.db) or temporary files created by some developer tools are typical candidates for ignoring globally.您应该小心选择全局忽略文件中的模式,因为不同的文件类型与不同的项目相关。一些开发工具创建的特殊操作系统文件(例如 .DS_Store和thumbs.db)或临时文件是典型的被全局忽略的候选文件。 一定要注意全局忽略文件的匹配一定不要.gitignore产生冲突

Ignoring a prevIoUsly committed file

If you want to ignore a file that you've committed in the past,you'll need to delete the file from your repository and then add a .gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository,but will remain in your working directory as an ignored file.

$ echo debug.log >> .gitignore

$ git rm --cached debug.log
rm 'debug.log'

$ git commit -m "Start ignoring debug.log"

You can omit the --cached option if you want to delete the file from both the repository and your local file system

Committing an ignored file

It is possible to force an ignored file to be committed to the repository using the -f (or --force) option with git add:

$ cat .gitignore
*.log

$ git add -f debug.log

$ git commit -m "Force adding debug.log"

You might consider doing this if you have a general pattern (like *.log) defined,but you want to commit a specific file. However a better solution is to define an exception to the general rule:

$ echo !debug.log >> .gitignore

$ cat .gitignore
*.log
!debug.log

$ git add debug.log

$ git commit -m "Adding debug.log"

This approach is more obvIoUs,and less confusing,for your teammates.

Stashing an ignored file

git stash is a powerful Git feature for temporarily shelving and reverting local changes,allowing you to re-apply them later on. As you'd expect,by default git stash ignores ignored files and only stashes changes to files that are tracked by Git. However,you can invoke to stash changes to ignored and untracked files as well
Debugging .gitignore files

Debugging .gitignore files

If you have complicated .gitignore patterns,or patterns spread over multiple .gitignore files,it can be difficult to track down why a particular file is being ignored. You can use the git check-ignore command with the -v (or --verbose)较详细的信息 option to determine which pattern is causing a particular file to be ignored:

$ git check-ignore -v debug.log
 .gitignore: 3 : *.log  debug.log

The output shows:

 :  :  

You can pass multiple file names to git check-ignore if you like,and the names themselves don't even have to correspond to files that exist in your repository.

More About

Xcode内置的git全局忽略文件路径:~/.gitignore_global
SourceTree:默认的全局忽略文件路径:.gitignore_global 针对每个工程的忽略文件是.gitignore 这两个文件的路径都是可以指定的
在写匹配模式时应该特别小心:要注意:logs,logs/,logs/debug.log,**/logs/debug.log区别;如果出错可以使用git check-ignore -v 来进行测试

参考内容


猜你在找的Git相关文章