Git .gitignore configuration ---- 2016-06-28

[Original address:]  http://sentsin.com/web/666.html

own example:

/target/

/.settings/

.project

.classpath

/log/

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!

最后需要强调的一点是,如果你不慎在创建.gitignore文件之前就push了项目,那么即使你在.gitignore文件中写入新的过滤规则,这些规则也不会起作用,Git仍然会对所有文件进行版本管理。

简单来说,出现这种问题的原因就是Git已经开始管理这些文件了,所以你无法再通过过滤规则过滤它们。

所以大家一定要养成在项目开始就创建.gitignore文件的习惯,否则一旦push,处理起来会非常麻烦。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326802680&siteId=291194637