❤️ Git Development Essentials ❤️. gitignore details! [suggested collection]

Posted by hypertech on Mon, 20 Dec 2021 09:57:06 +0100

🎈 Author: Linux ape

🎈 Introduction: CSDN blog expert 🏆, C/C + +, interview, question brushing and algorithm. Please consult me, pay attention to me and chat privately if you have any questions!

🎈 Attention column: Linux (high quality articles are constantly updated...) 🚀

catalogue

1, Why gitignore ?

2, Use rules

2.1 notes

2.2 ignore files

2.3. Ignore directory

2.4} use wildcards

2.5 reverse operation

2.6 double star

2.7 other rules

3, Summary

In managing projects using git gitignore file is a necessary file. Let's talk about it in detail!

1, Why gitignore ?

In some projects, we do not want to upload all files from the local warehouse to the remote warehouse, but selectively upload them, For example, some dependency files (dependencies under node_modules), files under bin directory, test files, etc. on the one hand, some dependency and test files are uploaded to the remote, with a large amount of transmission. On the other hand, some files are available to you, but not to another person, such as local configuration files.

In order to solve the above problems, git introduces gitignore file, which is used to selectively upload files.

2, Use rules

2.1 notes

A comment begins with # a comment followed by the content of the comment. As follows:

linuxy@linuxy:~/linuxGit$ cat .gitignore 
# this is .gitignore file.
# The following files are ignored
out
*.exe
linuxy@linuxy:~/linuxGit$

In the above example, comments begin with #.

2.2 ignore files

(1) Ignore files and directories

For example: folderName: indicates that the folderName file and folderName directory are ignored, and multi-level directories will be automatically searched, such as: * / * / folderName.

Let's take a simple example. The directory structure of the local warehouse is as follows:

linuxy@linuxy:~/linuxGit$ tree
.
├── folder
│   └── file1
└── src
    ├── folder
    └── utils
        └── folder

3 directories, 3 files
linuxy@linuxy:~/linuxGit$

Of which The contents of gitignore file are as follows:

linuxy@linuxy:~/linuxGit$ cat .gitignore 
# this is .gitignore file.
# The following files are ignored

folder
linuxy@linuxy:~/linuxGit$ 

Therefore, in the local warehouse, the folder directory, src/folder file and src/utils/folder file with the same name will be ignored, that is, they will not be submitted to the remote warehouse.

(2) Ignore files only

The mode is as follows:

folderName

!folderName/

Only the folderName file is ignored, not the folderName directory, where the exclamation point is "!" Indicates reverse operation.

Let's take a simple example. The directory structure of the local warehouse is as follows:

linuxy@linuxy:~/linuxGit$ tree
.
├── folder
│   └── file1
└── src
    ├── folder
    └── utils
        └── folder

3 directories, 3 files
linuxy@linuxy:~/linuxGit$

Of which The contents of gitignore file are as follows:

linuxy@linuxy:~/linuxGit$ cat .gitignore 
# this is .gitignore file.
# The following files are ignored

folder
!folder/
linuxy@linuxy:~/linuxGit$

Therefore, in the local warehouse, src/folder files and src/utils/folder files will be ignored, while folder directories with the same name will not be ignored.

2.3. Ignore directory

The mode is as follows:

folderName/

Ignore the folderName directory, including:

(1) folderName in the current directory, for example: folderName /;

(2) folderName under multi-level directory, for example: * / * / folderName /;

Let's take a simple example. The directory structure of the local warehouse is as follows:

linuxy@linuxy:~/linuxGit$ tree
.
├── folder
│   └── file1
└── src
    ├── folder
    └── utils
        └── folder

3 directories, 3 files
linuxy@linuxy:~/linuxGit$

Of which The contents of gitignore file are as follows:

linuxy@linuxy:~/linuxGit$ cat .gitignore 
# this is .gitignore file.
# The following files are ignored

folder/
linuxy@linuxy:~/linuxGit$

Therefore, in the local warehouse, the folder directory will be ignored, while the src/folder file and src/utils/folder file with the same name will not be ignored.

2.4} use wildcards

Common wildcards are:

(1) Asterisk "*": matches multiple characters;

(2) Question mark "?": Match any character except '/';

(3) Square brackets "[xxxx]": match characters in multiple lists;

Let's take a simple example. The directory structure of the local warehouse is as follows:

linuxy@linuxy:~/linuxGit$ tree
.
├── src
│   ├── add.c
│   ├── add.i
│   └── add.o
├── test.c
├── test.i
└── test.o

1 directory, 6 files
linuxy@linuxy:~/linuxGit$

Of which The contents of gitignore file are as follows:

linuxy@linuxy:~/linuxGit$ cat .gitignore 
# this is .gitignore file.
# The following files are ignored

*.[io]
linuxy@linuxy:~/linuxGit$

Therefore, in the local warehouse, test I file, test O file, Src / add O file, Src / add I file will be ignored and test C file and add C files are not ignored. Note: the matching pattern ignored here is multi-level directory.

2.5 reverse operation

The mode is as follows:

!Matching pattern 

Indicates that the previously ignored matching pattern is included in the trace content again.

For example, the mode mentioned when ignoring only files:

folderName

!folderName/

Indicates that only the folderName file is ignored, not the folderName directory.

2.6 double star

A slash followed by two consecutive asterisks (* *) indicates a multi-level directory.

Let's take a simple example The contents of gitignore file are as follows:

linuxy@linuxy:~/linuxGit$ cat .gitignore 
# this is .gitignore file.
# The following files are ignored

src/**/file
linuxy@linuxy:~/linuxGit$

It can mean ignoring src/folder1/file, src/folder1/folder2/***/foldern/file, etc.

2.7 other rules

(1) Blank lines do not match any files;

(2) git tracks files, not directories;

(3) In the. gitignore file, each line represents a pattern;

(4) If the local warehouse file has been tracked, it does not work even if ignore is set in. gitignore.

(5) The. gitignore file will also be uploaded to the remote warehouse, so people in the same warehouse can use the same. gitignore file.

3, Summary

In the process of using git, master The use of gitignore is important to reduce unnecessary file uploads to remote sites.

🎈 This article is original and difficult to create. If it is helpful to you, you are welcome to praise it 👍, Collection ⭐, Leaving a message. 💬

⭐ Haowen recommendation ⭐

[ten thousand words] ❤️ 8 sorting algorithms ❤️ [suggested collection]

Master C/C + + memory leakage, prevent memory leakage and detection tools!

Wild pointer? Dangling pointer? A text will help you understand!

Tetris game, revisit the classic (C + + detailed version)

Topics: Linux git github GitLab