Hello and welcome to Git Monday.

Today, I’m going to talk to you about gitignore. In fact, if you can speak English well, you should have guessed its function basically. Gitignore — ignore — git So gitignore helps us to automatically exclude certain files we specify when git add and not commit them to Git.

Ignore the role of

Git was created to manage code and files, so why prohibit some file submissions?

It’s actually very simple. Let me give you a very simple example. For example, when we write compiled languages such as C, C++, Java, go. We usually compile them before we run them into an executable binary file, also called a binary file. The compiled binary file is actually large, much larger than the source code. It’s not uncommon for dozens of lines of code to compile hundreds of kilobytes or even 1MB, so there’s no need to upload binaries like these.

Because whoever gets their hands on the source code and compiles it can get the same results, there is no need to upload the executable. Instead, it takes up a lot of space in the Git repository, where it will remain no matter how many iterations the project goes through, taking up disk resources.

There are many other files that don’t need to be uploaded, such as Java compiled class files, intermediate results, configuration files, etc. Git Add is often used when submitting files with git Add. That is, submit all the commands to submit. However, if we have any content in our directory that we don’t want to commit, we will commit it all, so we want to be able to set it in one place so that we can use Git add. We can avoid some documents we don’t want to submit.

The place to do this is the gitignore file.

Method of use

Gitignore is easy to use. Create a.gitignore file in your git directory and list the files you don’t want to commit.

Any names listed in this file will be ignored for us when we use Git add. There is no need to write the gitignore file from scratch, because git already has a lot of templates written for us that we can refer to directly.

The template address: https://github.com/github/gitignore

When we open it, we see various ignore files, which are gitignore files customized for each language.


Let’s just open up one of them. Like opening a Golang:


As you can see from the comments, these are mainly compiled results.

Set the ignore

The syntax of gitignore file can be boiled down to 5 simple rules, which are quite simple, let’s go through them one by one.

# comment

This one is easy enough to say, but # lines are ignored by Git because they represent comments. This annotation method is the same as Python and shell scripts, and should be familiar to those who have written it.

Ignore the entire folder

If we want to ignore everything in a folder under a path, we can write out the folder’s path followed by a slash.

For example, if we wanted to filter out all content in the SRC /build folder, we could say:

src/build/
Copy the code

So everything in this path is ignored, and this path is relative.

! Said the not

As mentioned, we can write a path to a folder and ignore all files in that folder. What if there is a file in that folder that we want to commit?

We can use it! Command to invert the name of the file we specified that can ignore the condition.

For example, if we want to keep the file named main.go in the SRC /build directory, we can add a line:

! main.goCopy the code

Glob pattern matching

We all know that regular expressions are complicated, and the introduction to Regular Expressions book is hundreds of pages long. But we don’t usually use this kind of complex pattern matching, so the simplified pattern matching rules we use in shell commands are much simpler than regular expressions.

For example, * can represent any string, which can be zero or any number of characters. [ABC] matches any character in square brackets,? Matches any character. [0-9] indicates any digit from 0-9. Two asterisks indicate any intermediate directory, such as SRC /**/build, which can match SRC /test/build or SRC /current/build.

If you want to filter out all TXT files, you can say:

*.txt
Copy the code

For example, if you want to filter out all json files in the build path, you can write:

build/*.json
Copy the code

Glob pattern matching is not nearly as powerful as regular expressions, but it is sufficient for common file-matching scenarios. Using these rules flexibly is enough to handle file ignore in almost any scenario.

We have written so much, but there are only a few key points, the cost of learning is not high, but after learning it can solve many problems once and for all, so I personally think it is very cost-effective, very worth learning.

Finally, I sincerely wish you all success every day. If you still like today’s content, please join us in a three-way support.

Original link, ask a concern

This article is formatted using MDNICE

– END –