Git ignore the use of file .gitignore

Original link: https://www.jianshu.com/p/a09a9b40ad20

1. Why should we ignore add?

When you use the git add .time you do not want to have encountered filings also added to the cache go? For example, local configuration of project information, if you upload it to go Git pull other people down, and his time will be local configuration there is a conflict, so this personalized profile we generally do not push it to the git servers, but it is each time you add cache to be lazy when want to use git add .to add a file instead of a manual, how to do it? Very simple, git provides a .gitignore file those documents as long as stated in this document that you do not want to add to the git go for us, so that when you use git add .the time these files will be automatically ignored.

2. Principles ignore file

  • 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.

3. Use

First, create a name for your workspace .gitignorefile.
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. All configuration files can be directly viewed online: https://github.com/github/gitignore

4. Examples

For example, your project is a java project, .javathe file will be compiled to generate .classfiles, these files are in most cases do not want the file to be transmitted to the warehouse. This time you can apply directly github of .gitignore file template. https://github.com/github/gitignore/blob/master/Java.gitignore copy these files to ignore information in your file to .gitignore:

*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

We can see github provides the most popular .gitignore profile to us.
After you save the file .ignore we look at git status, whether or not there we do not need the files will be added to the git go under examination:

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   .gitignore
        new file:   HelloWorld.java

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        Config.ini

For example, there is a file Config.ini my project directory, this is a local profile I do not want to upload to the git go, we can add this in gitignore configuration file:

Config.ini

Or if you want to ignore all the .ini files you can write:

*.ini

If the file has been somewhat neglected you, when you use git addthe time can not be added, for example, I ignored *.class, and now I want HelloWorld.classto add to the git go:

$ git add HelloWorld.class
The following paths are ignored by one of your .gitignore files:
HelloWorld.class
Use -f if you really want to add them.

git will prompt us to this file has been ignored us, you need to add -fparameters to forcibly added to the git go:

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   .gitignore
        new file:   HelloWorld.class
        new file:   HelloWorld.java

This will forcibly added to the cache go.
If we ignore the unexpected will want to add files to the cache go, we can use the rmcommand from which it was removed:

$ git rm HelloWorld.class --cached
rm 'HelloWorld.class'

If you do not want to have to upload files uploaded to the git repository, then you have to start with a remote repository delete it, then we can pull the code to delete the local repository of these documents will be deleted from this remote repository directly, or delete these files from the local and add the files you want to ignore in .gitignore file, and then push to the remote repository.

5. Check gitignore rules

If you issued .gitignorewritten there is a problem, you need to find out which rule was wrong in the end, can be used git check-ignoreto check the command:

$ git check-ignore -v HelloWorld.class
.gitignore:1:*.class    HelloWorld.class

We can see the HelloWorld.classmatch to our first *.classignore the rules so the file is ignored.

6. Ignore the rules file syntax

a. Ignore the specified file / directory

# 忽略指定文件
HelloWrold.class

# 忽略指定文件夹
bin/
bin/gen/

b. Wildcard ignored rules

Wildcard rules are as follows:

# 忽略.class的所有文件
*.class

# 忽略名称中末尾为ignore的文件夹
*ignore/

# 忽略名称中间包含ignore的文件夹
*ignore*/

Reference material

https://www.jianshu.com/p/a09a9b40ad20

Guess you like

Origin blog.csdn.net/tianxintiandisheng/article/details/102761775