Skip to content

Instantly share code, notes, and snippets.

@montasim
Last active July 6, 2024 14:17
Show Gist options
  • Save montasim/6ad82389014aaa6fd43a35d0766b4361 to your computer and use it in GitHub Desktop.
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 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.
# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto
# Ensures automatic text handling, which normalizes line endings on commit and converts them on checkout.
# Specify line ending settings for specific file types to ensure consistency across platforms.
*.js text eol=crlf # Ensure JavaScript files use CRLF line endings in Windows environments.
*.jsx text eol=crlf # Ensure JSX files use CRLF line endings in Windows environments.
*.ts text eol=crlf # Ensure TypeScript files use CRLF line endings in Windows environments.
*.tsx text eol=crlf # Ensure TSX files use CRLF line endings in Windows environments.
*.json text eol=crlf # Ensure JSON files use CRLF line endings in Windows environments.
*.md text eol=crlf # Ensure Markdown files use CRLF line endings in Windows environments.
# Set UTF-8 encoding for certain file types to prevent encoding issues across different environments.
*.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 to prevent issues with formatting that uses trailing whitespace.
*.md text=auto eol=crlf
*.md -trim_trailing_whitespace # Disable trimming of trailing whitespace in Markdown files.
# Define binary files to prevent Git from attempting to auto-correct line endings or apply text transformations.
*.png binary # Treat PNG files as binary to avoid corrupting their content.
*.jpg binary # Treat JPEG files as binary to avoid corrupting their content.
*.gif binary # Treat GIF files as binary to avoid corrupting their content.
# Explanation of binary setting
# 'binary' attribute is used to tell Git to treat the file as a binary data, preventing any automatic end-of-line conversion,
# and ensuring that the file is not modified.
@montasim
Copy link
Author

montasim commented Jul 6, 2024

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:

  • text=auto: Sets a default behavior for all files, letting Git handle line endings automatically. This helps normalize line endings on commit and converts them back on checkout, preventing issues when the code is run on different operating systems.
    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment