.gitignore details

Today I will talk about a very important file in Git - .gitignore.

First of all, it should be emphasized that the full file name of this file is ".gitignore", note that there is a "." at the front. This file without an extension is not easy to create under Windows. Here is the creation method of win7:

Create a file named: ".gitignore.", note that there is a dot before and after. After saving, the system will automatically rename it to ".gitignore".

Generally speaking, every Git project needs a ".gitignore" file. The function of this file is to tell Git which files do not need to be added to version management.

In actual projects, many files do not require version management, such as Python .pyc files and some configuration files containing passwords, etc.

The contents of this file are the rules that Git uses to decide whether to add the file to version control.

Let's take a look at the commonly used rules:

  • /mtk/ filter the entire folder

  • *.zip filter all .zip files

  • /mtk/do.c filter a specific file

It's very simple, the filtered files will not appear in your GitHub repository, of course, there are still in the local repository, but they will not be uploaded when you push.

Note that gitignore can also specify which files to add to version management:

  • !*.zip

  • !/mtk/one.txt

The only difference is that there is an exclamation mark at the beginning of the rule, and Git will add files that meet such a rule to version management.

Why have two rules? Imagine a scenario: we only need to manage the one.txt file in the /mtk/ directory, and no other files in this directory need to be managed. Then we need to use:

  • /mtk/

  • !/mtk/one.txt

Assuming that we only have filtering rules and no rules are added, then we need to write all the files in the /mtk/ directory except one.txt!

The last thing to emphasize is that if you accidentally push the project before creating the .gitignore file, then even if you write new filter rules in the .gitignore file, these rules will not work, Git will still File version management.

Simply put, the reason for this problem is that Git has already started managing these files, so you can no longer filter them through filter rules.

So everyone must develop the habit of creating the .gitignore file at the beginning of the project, otherwise it will be very troublesome to deal with once pushed.

 

# 此为注释 – 将被 Git 忽略
 
*.a       # 忽略所有 .a 结尾的文件
!lib.a    # 但 lib.a 除外
/TODO     # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
build/    # 忽略 build/ 目录下的所有文件
doc/*.txt # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt

Guess you like

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