When using Git for version control, we often need to ignore certain files or directories, such as temporary files generated by compilation, log files, etc., to avoid including them in the version library. Git properties and ignore files are two commonly used mechanisms for controlling the tracking and ignoring of files. This article explains how to use Git properties and ignore files to manage files in your project.
Git properties
Git attributes can help us define file-specific attributes and perform special processing based on these attributes. It uses .gitattributes
files to define attribute rules. Here are some commonly used Git properties:
text
: Mark the file as a text file, and Git will try to automatically convert line breaks and other operations when processing the file.binary
: Mark the file as a binary file, and Git will process the file in binary mode without any conversion.merge=union
: When merging branches, for files with the same path but different content, merge the file content into a new file instead of selecting a branched version.merge=ours
: When merging branches, always select the current branch's version for files with the same path but different content.merge=theirs
: When merging branches, for files with the same path but different content, always pick the other branch's version.
To use Git properties in your project, you first need to create .gitattributes
a file. In this file, we can use path matching patterns to specify which attribute rules to apply. For example, suppose we want to .txt
mark all files as text files, we can .gitattributes
add the following rule to the file:
*.txt text
In this way, Git will .txt
automatically perform corresponding text processing when processing files.
ignore file
Some files or directories we want to ignore completely, i.e. not take them into version control. To achieve this, we can use .gitignore
files to specify files and directories to ignore. .gitignore
The file uses simple rules to describe what to ignore.
Here are some .gitignore
examples of common rules for files:
*.log
: Ignore all files.log
with extensions./build/
: Ignorebuild
directories named under the root directory.temp/
: Ignore alltemp
directories named regardless of their location.
Note that .gitignore
regular expressions can be used in the file for more complex matching.
After creating .gitignore
the file and adding the appropriate rules, Git automatically
Matching files and directories are ignored. They will not appear in the results of git status
the or git add
command.
example
Here is an example .gitattributes
and .gitignore
the contents of the file:
.gitattributes
# 将所有.txt文件标记为文本文件
*.txt text
# 对于.conf文件,始终选择当前分支的版本
*.conf merge=ours
# 对于所有图片文件,将内容合并为新的文件
*.png merge=union
*.jpg merge=union
.gitignore
# 忽略所有以.tmp为扩展名的文件
*.tmp
# 忽略所有名为logs的目录
/logs/
# 忽略根目录下的config.ini文件
/config.ini
# 忽略当前目录下的temp目录,以及任意层级的temp目录
temp/
.gitattributes
Using and files in the project .gitignore
allows for better control over file versioning and ignoring, ensuring that unnecessary files and directories do not enter the repository.
This article covers advanced operations with Git properties and ignore files. By using .gitattributes
file and Git attributes, we can define file-specific attribute rules. At the same time, .gitignore
files help us specify files and directories to ignore, thus avoiding them being brought into version control.
Using Git properties and ignoring files can help us better manage and control the files in the project and keep the repository clean. In actual projects, according to specific needs, we can set corresponding attribute rules and ignore rules according to file types, paths, etc.
continue reading: