The `.gitattributes` file is used to define attributes for paths in your Git repository. It allows you to specify how certain file types should be handled, such as end-of-line normalization, merge strategies, and diff settings. This tutorial covers the basics of creating and using a `.gitattributes` file, common patterns, and best practices.
The `.gitattributes` file is a simple text file that defines attributes for paths in your Git repository. These attributes can control how Git processes and treats certain files, including line endings, binary files, and custom merge drivers.
Note: The `.gitattributes` file is typically placed in the root of your repository, but you can also place it in any directory to apply attributes to files within that directory and its subdirectories.To create a `.gitattributes` file, simply create a new file named `.gitattributes` in the root directory of your repository. You can then add attribute definitions to this file.
# Creating a .gitattributes file:
touch .gitattributes
# Add attribute definitions to the .gitattributes file:
*.txt text
*.jpg binary
This example demonstrates how to create a `.gitattributes` file and add attribute definitions to it.
One of the most common uses of the `.gitattributes` file is to normalize line endings across different operating systems. You can use the `text` attribute to achieve this.
# Normalizing line endings:
# Convert all text files to LF on commit and check out according to platform
* text=auto
This example shows how to normalize line endings in your repository.
You can mark files as binary in the `.gitattributes` file to prevent Git from attempting to process them as text files. This is useful for images, executables, and other non-text files.
# Marking files as binary:
# Treat all .jpg files as binary
*.jpg binary
This example demonstrates how to mark files as binary in your repository.
The `.gitattributes` file allows you to customize the diff behavior for certain file types. You can define custom diff drivers to handle specific file formats.
# Customizing diff behavior:
# Use a custom diff driver for .md files
*.md diff=markdown
This example shows how to customize the diff behavior for specific file types.
You can define custom merge drivers in the `.gitattributes` file to handle conflicts for specific file types. This is useful for files that require special handling during merges.
# Customizing merge behavior:
# Use a custom merge driver for .xml files
*.xml merge=xml_merge
This example demonstrates how to customize the merge behavior for specific file types.
The `export-ignore` attribute allows you to exclude certain files from archive files created with `git archive`. This is useful for excluding development files, documentation, or other files not needed in the distributed package.
# Setting export-ignore:
# Ignore test files when creating an archive
test/ export-ignore
This example shows how to use the `export-ignore` attribute in the `.gitattributes` file.
You can force attributes to apply to specific files or patterns using the `!` (negate) syntax in the `.gitattributes` file. This can be useful for overriding default behaviors.
# Forcing attributes:
# Force text conversion for all .txt files
*.txt !eol
This example demonstrates how to force attributes for specific files or patterns.
The `.gitattributes` file allows you to configure Git to ignore white space changes in specific files during diffs and merges. This can be useful for code files where white space changes are common but do not affect functionality.
# Ignoring white space changes:
# Ignore white space changes in .js files
*.js -whitespace
This example shows how to ignore white space changes in specific files.
You can configure Git to handle text conversions for specific file types in the `.gitattributes` file. This is useful for converting line endings, encoding, or other text transformations.
# Handling text conversions:
# Convert line endings for .sh files to LF on commit and check out as-is
*.sh eol=lf
This example demonstrates how to handle text conversions in your repository.
Follow these best practices to make the most of your `.gitattributes` file:
The `.gitattributes` file is a powerful tool for defining how Git processes and treats different files in your repository. By understanding how to create, use, and manage `.gitattributes` files effectively, you can customize Git's behavior to suit your project's needs. This tutorial covered the basics of `.gitattributes`, with detailed explanations and examples to help you master this essential skill.