.gitignore use

First, Jane Shao

Each Git project we all need to do a ".gitignore" file, the role of this document is to tell Git which files need to add to the version management. For example, our project npm package (node_modules), which is very important in our project, but it accounts for memory is great, so when we use Git general management is not necessary to add npm package.

Second, the general rule

Git ignore files of principle
  • Ignore the operating system automatically generated files, such as thumbnails;
  • Ignored by the compiler generates intermediate files, executable files, etc., that is, if a file is automatically generated by another file that is automatically generated files into the repository is not necessary, such as Java compiled .class files generated;
  • Ignore your own configuration files with sensitive information, such as passwords stored in the configuration file.
Priority .gitignore ignore rules

In .gitingore file, each line specifies a rule ignored, Git ignore rule check when a plurality of sources, its priority as follows (highest to lowest):
1) available in the command line is read ignore rule
2) the rules defined in the current directory
3) the parent directory defined rules, in order recursive
4) rules $ GIT_DIR / info / exclude file defined in
5) global rules defined in core.excludesfile

.Gitignore ignored matching syntax rules

In .gitignore file, ignoring the syntax rules each line as follows:
1) Space does not match any file can be used as the separator, usable backslash

2) a " # " at the beginning of the line will be ignored Git. That is the beginning of the file # comment identifier, you can use the backslash escape.

3) can use the standard glob matching mode. Glob mode refers to the so-called simplified shell used regular expressions.

4) slash " / " at the beginning indicates a directory; "/" end of pattern matching only the contents of the folder and in that folder path, but does not match the file; a "/" pattern matching with the project directory; if mode does not contain a slash, it matches with respect to the contents of the current file path .gitignore, if the pattern is not .gitignore file, relative to the project root directory.

5) to "*****" wildcard characters plurality of asterisks, i.e., to match any number of characters; two asterisks "******" means match any intermediate directory, for example a/**/zto match a / z , a / b / z or a / b / c / z like.

"6) question mark ? " Single wildcard character, i.e. matches one arbitrary character;

7) in square brackets " [] " Match List contains a single character, i.e. matches any character a in square brackets. For example [abc] represents a match or a, B is either a match, a match or C; if the two characters separated by dashes used in the square brackets, all of which may represent two characters match within a range. Such as [0-9] which matches all numbers 0 to 9, [az] represented match any lowercase).

8) with an exclamation mark " ! " Means not ignore the (track) to match the file or directory that you want to ignore the file or directory other than the specified mode, you can add an exclamation point in the current mode (!) Negated. Special attention is needed: if the parent directory of the file has been ruled out of the front of the rules, then the rules of this file does not work "!" . That "!" At the beginning of the pattern indicates negative, the file will be included again, if you exclude the parent directory of the file, use the "!" Will not be included again. You can be escaped with a backslash.

Note: git for .ignore profile is matched by line from top to bottom rule, meaning if a wider range of previous rules match, then the rule will not take effect later;

Create a file .gitignore

1) a conventional windows operating

  • Creating gitignore.txt the root directory;
  • Edit gitignore.txt, write down your rules, for example, plus node_modules /;
  • Open a command line window, change to the root directory (folder can directly input the address bar above the transport cmd);
  • Run ren gitignore.txt .gitignore.

2) Git Bash

  • Right Select the root directory of "Git Bash Here" to enter the bash command window;
  • Input vim .gitignoreor touch .gitignorecommand, open the file (no file is automatically created);
  • Press the edit key to switch to state i, the input rule, e.g. node_modules /, and then press Esc to exit editing the: wq save and exit.
Use .gitignore file

First, create your workspace a name for .gitignore file.
Then, fill in the name of the file you want to ignore into, Git will automatically ignore these files.
.Gitignore do not need to re-write the file, GitHub has prepared a variety of configuration files for us, just a combination of what you can use.

Sometimes for some files under git project, we do not under version control, such as log files or IDE configuration file, then you can create a hidden file .gitignore (under linux to the root directory of the project. The beginning of the document are It is a hidden file), and then write the file to be ignored in .gitignore in.

[root@dalin ~]``# cat .gitignore
*.xml
*.log
*.apk

.gitignore comments with '#', * which matches zero or more of any character, so the above pattern is to ignore all the xml files, log files, and apk files.

.gitignore configuration file used to configure the file does not need to join version management, configure the file can bring great convenience for version management.

Example shows

a, rule: fd1 /
Description: Ignore all the contents of the directory fd1; note, whether it is / fd1 / directory in the root directory or a subdirectory / child / fd1 / directory will be ignored;
b, the rules: / fd1 /

Description: Ignore the root directory all content / fd1 under the / directory;
c, rules:

 !.gitignore
 !/fw/bin/
 !/fw/sf/

Description: Ignore all the content, but do not ignore .gitignore file, / fw in the root directory / bin / and / fw / sf / directory;

Quickly import .gitignore module

For a file or a git project, usually one or more specific language is, in fact, there is a write process can be simplified .gitignore, or that provide an idea to ignore us, that is relevant on githua the project, to have a list of all the common language of some commonly ignore the rules.
First, it provides a link to github: gitignore

of course not to say that these documents do not need to add in your own writing, as has its own specific files generated under the project or not the development environment, you can make the configuration file, it may be an intermediate file these files are often not need to upload into our repository, which requires us to ignore manually add specific rules.


If you accidentally before creating .gitignore to push the project file, even if you write a new filtering rule in .gitignore file, these rules will not work, Git will still manage all file versions. The reason for this problem is simple Git has started to manage these files, so you will not be able to filter them through a filter rule. Therefore, we must develop .gitignore file is created in the project get used to, otherwise a single push, dealing with them will be very troublesome.
.

Guess you like

Origin www.cnblogs.com/HalfCircle/p/9f58ee83a846c941bb151211150423cf.html