Git exclude directories

In Git version control, some files do not need to be controlled, so we need to ignore these files when submitting code. Let's talk about how to configure some ignore rules for Git.

 

There are three ways to ignore these files, all three methods can achieve the purpose, but the application situation is different.

 

1. Exclude files for a single project

In this way, all the modifiers of this project can clone the filter rules while cloning the code, without having to write another copy, which ensures that all modifiers apply the same rules, not Zhang San has his own set of filtering rules, and Li Si uses another set of filtering rules. The configuration steps are as follows:

Create a .gitignore file in the project root directory, and write the files or directories to be excluded to the .gitignore file. There are two writing methods:

(a) Use the command line to add an exclusion file (windows platform)

Exclude files ending with .class  

echo *.class>.gitignore

  (>> is added at the end of the file, > is added after deleting the existing content), and then a .gitignore file will be generated in the current directory.

 

(b) The most convenient way is to open it with Notepad, add the files or directories to be excluded, and add one per line, such as:

# maven ignore
target/
*.jar
*.war
*.zip
* .tar
* .tar.gz

# eclipse ignore
.settings/
.project
.classpath

# idea ignore
.idea/
*.ipr
*.iml
* .iws

# temp ignore
*.log
*.cache
*.diff
*.patch
*.tmp

# system ignore
.DS_Store
Thumbs.db

 

 

2. Global settings exclude files

This will work globally, as long as it is a project managed by Git, files or directories that are not under control will be automatically excluded when submitting. This method is more convenient for developers, as long as the global configuration is needed once, there is no need to configure the filtering rules every time a project is created. But this does not guarantee that after other developers clone your code, their rules are the same as yours, which brings about various conflicts in the code submission process. The configuration steps are as follows:

 (a) Like method (1), it is also necessary to create a .gitignore file and write the files to be excluded.

 

  (b) But here, we do not stipulate that the .gitnore file must be placed under a certain project, but anywhere. For example, we put it under the default Home path of Git, which is C:\Users on my windows. \zhbpeng

 

   (c) Use the command method to configure the global exclusion file git config --global core.excludesfile ~/.gitignore, you will find that excludesfile = c:/Users/zhbpeng/.gitignore will appear in the ~/.gitconfig file. Indicates that Git applies the file filtering rules to the Global rules.

 

3. Exclude files for individual project settings

In the project directory vi .git/info/exclude, write the files to be excluded:

  *.class
  *.apk
  bin/
  gen/
  .settings/
  proguard/

 

This method is not recommended, it can only be configured for a single project, and the filtering rules cannot be synchronized to other developers. Compared with methods (1) and (2), there is no advantage at all.

 

demo settings:

*.[oa] #Ignore files or directories ending with .o or .a
*.pyc        
*.exe #Ignore files or directories ending with .exe
.* #Ignore files or directories starting with .
*.rar
*.zip
*.gz
*.bz2
*.db
*.sqlite

 

Syntax rules for ignored files in Git

Ignore blank lines in the file or lines starting with a pound sign (#) will be ignored.

Linux wildcards can be used. For example: an asterisk (*) represents any number of characters, a question mark (?) represents a character, square brackets ([abc]) represent optional character ranges, and curly brackets ({string1,string2,...}) represent optional string etc.

If there is an exclamation mark (!) at the front of the name, it indicates an exception rule and will not be ignored.

If the name is preceded by a path separator (/), it means that the files to be ignored are in this directory, and the files in subdirectories are not ignored.

If the name is followed by a path separator (/), it means that the subdirectory of the name in this directory is to be ignored, not the file (the default file or directory is ignored).

 

Example:

# This is a comment line and will be ignored
*.a # Ignore all files with .a extension    
!lib.a # But the file or directory named lib.a should not be ignored, even if the previous setting to ignore *.a
/TODO # Only ignore TODO files in this directory, TODO files in subdirectories are not ignored
build/ # Ignore all files in the build directory, but not if it is a file named build
doc/*.txt # files such as doc/notes.txt are ignored, but files such as doc/server/arch.txt are not ignored

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326305251&siteId=291194637