Last active
August 8, 2024 17:14
-
-
Save montasim/6ad82389014aaa6fd43a35d0766b4361 to your computer and use it in GitHub Desktop.
Using a .gitattributes file improves the robustness of a codebase by ensuring that all contributors adhere to defined file handling conventions, regardless of their personal Git configurations or operating systems. This leads to fewer merge conflicts and issues related to improper file handling, and it makes the repository more stable and easier…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#################################################### | |
# .gitattributes Configuration File | |
#################################################### | |
# This configuration file is used by Git to handle the line endings and encoding settings for files based on their type. | |
# It ensures consistent handling of files across different operating systems and environments. | |
#################################################### | |
# DEFAULT BEHAVIOR | |
# Purpose: Defines the default text handling behavior for files when committing and checking out. | |
# Use: Ensures automatic normalization of line endings. | |
#################################################### | |
* text=auto | |
# Ensures automatic text handling, which normalizes line endings on commit and converts them on checkout. | |
#################################################### | |
# FILE TYPE SPECIFIC LINE ENDING SETTINGS | |
# Purpose: Specifies line ending settings for specific file types to ensure consistency across platforms. | |
# Use: Configure the end-of-line normalization for different file types. | |
#################################################### | |
*.js text eol=crlf | |
*.jsx text eol=crlf | |
*.ts text eol=crlf | |
*.tsx text eol=crlf | |
*.json text eol=crlf | |
*.md text eol=crlf | |
#################################################### | |
# FILE TYPE SPECIFIC ENCODING SETTINGS | |
# Purpose: Sets UTF-8 encoding for certain file types to prevent encoding issues across different environments. | |
# Use: Configure file encoding to ensure consistent handling of character sets. | |
#################################################### | |
*.js charset=utf-8 | |
*.jsx charset=utf-8 | |
*.ts charset=utf-8 | |
*.tsx charset=utf-8 | |
*.json charset=utf-8 | |
#################################################### | |
# SPECIAL HANDLING FOR MARKDOWN FILES | |
# Purpose: Provides special handling for markdown files to avoid issues with formatting and trailing whitespace. | |
# Use: Ensure consistent formatting and handling of markdown files. | |
#################################################### | |
*.md text=auto eol=crlf | |
*.md -trim_trailing_whitespace | |
#################################################### | |
# BINARY FILES | |
# Purpose: Defines binary files to prevent Git from attempting to auto-correct line endings or apply text transformations. | |
# Use: Ensure binary files are treated as binary data without modification. | |
#################################################### | |
*.png binary | |
*.jpg binary | |
*.gif binary | |
#################################################### | |
# EXPLANATION OF BINARY SETTING | |
# Purpose: Ensures binary files are not modified by Git's end-of-line conversion or text transformations. | |
# Use: Maintain the integrity of binary files such as images. | |
#################################################### | |
# 'binary' attribute is used to tell Git to treat the file as binary data, preventing any automatic end-of-line conversion, | |
# and ensuring that the file is not modified. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Overview
The .gitattributes file directs Git on how to treat files in the repository. It can control various aspects such as line endings, encoding, and whether files should be treated as binary or text. The configuration helps avoid common issues like merge conflicts arising from inconsistent line endings among contributors using different operating systems.
File Descriptions
Default Text Handling:
Specific File Type Settings:
*.js text eol=crlf and other similar settings for *.jsx, *.ts, *.tsx, *.json, *.md: These ensure that specific file types use CRLF (Carriage Return and Line Feed) as line endings, which is standard for Windows environments. This specification helps maintain consistency, especially in cross-platform projects.
Encoding Settings:
*.js charset=utf-8 and similar settings for other file types: Ensures these files are consistently encoded in UTF-8 across all platforms, which supports a wide range of characters and symbols and prevents encoding issues.
Markdown Files Handling:
*.md text=auto eol=crlf: Ensures Markdown files handle line endings in a manner suited for text files but normalizes to CRLF on Windows.
*.md -trim_trailing_whitespace: Disables the trimming of trailing whitespace in Markdown files, which is crucial since Markdown uses trailing spaces to denote line breaks in certain contexts.
Binary Files:
*.png binary, *.jpg binary, *.gif binary: These settings ensure that image files are treated as binary data, which is essential to prevent Git from altering their content. Treating them as binary prevents Git from attempting to change line endings or apply text transformations, which could corrupt the files.