What is .gitattributes?
.gitattributes is a configuration file in the Git repository. It is used for defining paths and files in a Git repository. It defines “How git should treat certain files. This includes settings such as line endings. file encoding and merge strategies.
Syntax:
The syntax for .gitattributes is simple and follows a key-value format, where each line represents a path or pattern followed by its corresponding attributes.
pattern attribute=value
- pattern: It can be a file name, a path, or a glob pattern to match multiple files.
- attribute: It is the attribute you want to set in your after the pattern.
- value: It is the optional and depends on the attribute begin set.
Once you’ve defined the desired attributes in the “.gitattributes” file, save the changes and commit them to your repository like any other file.
Features of .gitattributes:
- Line Ending Conversion: Control how Git handles line endings, useful in cross-platform development.
- Merge Strategies: Define custom merge drivers or strategies for specific file types.
- Binary vs. Text Files: Mark certain files as binary to prevent Git from attempting to merge them.
- Linguist Language Override: Override language detection for specific files when using GitHub’s Linguist library.
- Custom Attributes: Set custom attributes for files based on their path or pattern.
Example:
*.txt text=auto
*.docx merge=word
*.png binary
README.md linguist-detectable=false
.gitattributes vs .gitignore in Git
Git is a command line tool. It is a distributed version control system(DVCS) widely used for tracking changes in source code during software development. It was created by Linux Torvalds in 2005 to manage the development of the Linux Kernel. In this post, we will understand the two git files .gitattributes and .gitignore.
Table of Content
- What is .gitattributes?
- What is a “.gitignore”?
- Difference between .gitattributes and .gitignore
- Conclusion