Git ignores files Use of gitignore

Posted by paparanch on Thu, 17 Feb 2022 05:56:47 +0100

This blog is designed for self-learning. If you have any questions, please contact the blogger in time


When you use git add Have you ever added files you don't want to submit to the cache? For example, if you upload the local configuration information of the project to git, it will conflict with the local configuration when others pull it down. Therefore, we generally do not push such a personalized configuration file to the GIT server, but in order to be lazy, we want to use git add every time we add the cache What about adding a file instead of one manually? Quite simply, GIT provides us with a gitignore file just state in this file that you don't want to add to git, so when you use git add These files will be automatically ignored when.

2. Principle of ignoring documents #

  • Ignore the files automatically generated by the operating system, such as thumbnails;
  • Ignore the intermediate files and executable files generated by compilation, that is, if a file is automatically generated through another file, it is not necessary to put the automatically generated files into the version library, such as those generated by Java compilation class file;
  • Ignore your own configuration files with sensitive information, such as those that store passwords.

3. Usage #

First, create a new name in your workspace gitignore file. Then, fill in the file names to be ignored, and Git will automatically ignore these files. There is no need to write from scratch gitignore file, GitHub has prepared various configuration files for us, which can be used only by combining them. All configuration files can be browsed directly online:

4. Chestnuts #

For example, your project is a java project Java files will be generated after compilation class files, which in most cases are files that do not want to be transferred to the warehouse. At this time, you can directly apply github gitignore file template. Copy these ignored file information to your In gitignore file:


# Mobile Tools for Java (J2ME)

# Package Files #

# virtual machine crash logs, see [](

You can see that github provides us with the most popular gitignore file configuration. preservation. After ignoring the file, let's check git status to see if there are any files we don't need that will be added to git:

$ git status
On branch master

Initial commit

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

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

For example, there is a config. In my project directory INI file, which is a local configuration file. I don't want to upload it to git. We can add such a configuration to gitignore file:


Or you want to ignore everything ini file you can write as follows:


If some files have been ignored by you, you can't add them when you use git add. For example, I ignored * Class, now I want to put HelloWorld Add class to git:

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

Git will remind us that this file has been ignored by us. You need to add the - f parameter to force it to be added to git:

$ 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:

In this way, it can be forcibly added to the cache. If we accidentally add a file that we want to ignore to the cache, we can use the rm command to remove it from the cache:

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

If you have uploaded the files you don't want to upload to the git warehouse, you must delete them from the remote warehouse first. We can delete them directly from the remote warehouse, and then pull the code to the local warehouse. These files will be deleted, or these files will be deleted from the local warehouse and displayed in the Add these files you want to ignore to the gitignore file, and then push them to the remote warehouse.

5. View gitignore rules #

If you send it There is something wrong with gitignore. You need to find out which rule is wrong. You can use git check ignore command to check:

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

You can see HelloWorld class matches our first * class, so the file was ignored.

6. Ignore rule file syntax #

a. Ignore specified file / directory #

# Ignore specified file

# Ignore specified folder

b. Wildcard ignore rule #

The wildcard rules are as follows:

# Ignore All files of class

# Ignore folders with ignore at the end of the name

# Ignore folders with ignore in the middle of the name