Skip to main content
Chapter 20 of 61 · 4 sections · Page 99

Git: Gitattributes

Configure automatic line ending normalization, binary file detection, and custom diff drivers.

Original chapter reference: Chapter 20: Using a .gitattributes file. The command options and documentation recipes below are fully transcribed from Git Notes for Professionals.

Section 20.1: Automatic Line Ending Normalization

Create a .gitattributes file in the project root containing:

Source example · PDF page 99
* text=auto

This will result in all text files (as identified by Git) being committed with LF, but checked out according to the host operating system default.

This is equivalent to the recommended core.autocrlf defaults of:

input on Linux/macOS true on Windows

Section 20.2: Identify Binary Files

Git is pretty good at identifying binary files, but you can explicitly specify which files are binary. Create a .gitattributes file in the project root containing:

Source example · PDF page 99
*.png binary

binary is a built-in macro attribute equivalent to -diff -merge -text.

Section 20.3: Prefilled .gitattribute Templates

If you are unsure which rules to list in your .gitattributes file, or you just want to add generally accepted attributes to your project, you can shoose or generate a .gitattributes file at:

https://gitattributes.io/ https://github.com/alexkaratarakis/gitattributes

Section 20.4: Disable Line Ending Normalization

Create a .gitattributes file in the project root containing:

Source example · PDF page 99
* -text

This is equivalent to setting core.autocrlf = false.

Buy me a coffee
Daily Practice
0/0