Git Lecture 16 Git properties and ignore files

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 .gitattributesfiles 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 .gitattributesa file. In this file, we can use path matching patterns to specify which attribute rules to apply. For example, suppose we want to .txtmark all files as text files, we can .gitattributesadd the following rule to the file:

*.txt text

In this way, Git will .txtautomatically 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 .gitignorefiles to specify files and directories to ignore. .gitignoreThe file uses simple rules to describe what to ignore.

Here are some .gitignoreexamples of common rules for files:

  • *.log: Ignore all files .logwith extensions.
  • /build/: Ignore builddirectories named under the root directory.
  • temp/: Ignore all tempdirectories named regardless of their location.

Note that .gitignoreregular expressions can be used in the file for more complex matching.

After creating .gitignorethe file and adding the appropriate rules, Git automatically

Matching files and directories are ignored. They will not appear in the results of git statusthe or git addcommand.

example

Here is an example .gitattributesand .gitignorethe 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/

.gitattributesUsing and files in the project .gitignoreallows 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 .gitattributesfile and Git attributes, we can define file-specific attribute rules. At the same time, .gitignorefiles 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:

Guess you like

Origin blog.csdn.net/huanglu0314/article/details/131157638