git ignore patterns

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangbingfengf98/article/details/85251402

.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
You can prepend a pattern with a double asterisk to match directories anywhere in the repository.
**/logs/debug.log logs/debug.log
build/logs/debug.log
but not
logs/build/debug.log
You can also use a double asterisk to match files based on their name and the name of their parent directory.
*.log debug.log
foo.log
.log
logs/debug.log
An asterisk is a wildcard that matches zero or more characters.
*.log
!important.log
debug.log
trace.log
but not
important.log
logs/important.log
Prepending an exclamation mark to a pattern negates it. If a file matches a pattern, but 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
Patterns defined after a negating pattern will re-ignore any previously negated files.
/debug.log debug.log
but not
logs/debug.log
Prepending a slash matches files only in the repository root.
debug.log debug.log
logs/debug.log
By default, patterns match files in any directory
debug?.log debug0.log
debugg.log
but not
debug10.log
A question mark matches exactly one character.
debug[0-9].log debug0.log
debug1.log
but not
debug10.log
Square brackets can also be used to match a single character from a specified range.
debug[01].log debug0.log
debug1.log
but not 
debug2.log
debug01.log
Square brackets match a single character form the specified set.
debug[!01].log debug2.log
but not
debug0.log
debug1.log
debug01.log
An exclamation mark can be used to match any character except one from the specified set.
debug[a-z].log debuga.log
debugb.log
but not
debug1.log
Ranges can be numeric or alphabetic.
logs logs
logs/debug.log
logs/latest/foo.bar
build/logs
build/logs/debug.log
If you don't append a slash, the pattern will match both files and the contents of directories with that name. In the example matches on the left, both directories and files named logs are ignored
logs/ logs/debug.log
logs/latest/foo.bar
build/logs/foo.bar
build/logs/latest/debug.log
Appending a slash indicates the pattern is a directory. The entire contents of any directory in the repository matching that name – including all of its files and subdirectories – will be ignored
logs/
!logs/important.log
logs/debug.log
logs/important.log
Wait a minute! Shouldn't logs/important.logbe 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/**/debug.log logs/debug.log
logs/monday/debug.log
logs/monday/pm/debug.log
A double asterisk matches zero or more directories.
logs/*day/debug.log logs/monday/debug.log
logs/tuesday/debug.log
but not
logs/latest/debug.log
Wildcards can be used in directory names as well.
logs/debug.log logs/debug.log
but not
debug.log
build/logs/debug.log
Patterns specifying a file in a particular directory are relative to the repository root. (You can prepend a slash if you like, but it doesn't do anything special.)

** 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).*

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.

For example a .gitignore file in the root directory with these contents:

/content/*.txt

Will ignore all .txt files in the content directory. It will not ignore .txt files in the subdirectories of the content directory. If you want to do that the only way I'm aware of is to create a .gitignore file in the content directory with the following contents:

*.txt

If you want to ignore .txt files only in the root directory of your repository you can use the following .gitignore file (placed in the root directory of your repository):

/*.txt

references:

1. https://www.atlassian.com/git/tutorials/saving-changes/gitignore

2. https://www.quora.com/Does-gitignore-file-only-work-on-current-folder

猜你喜欢

转载自blog.csdn.net/wangbingfengf98/article/details/85251402