THE GITHTML FIELD GUIDE

Set a shared line-ending policy with .gitattributes

A committed .gitattributes file can define text normalization and working-tree line endings for selected paths. Use text and eol attributes deliberately, inspect effective rules with git check-attr, and separate any normalization from functional changes. Adding attributes does not justify blindly restaging every file in a checkout that contains unrelated work.

Distinguish stored text from working copies

Git can normalize text to LF in the repository while checking it out with selected line endings. This separates the stored representation from what tools on a particular platform need. The text attribute controls normalization behavior, while eol can specify a working-tree convention for matching files. Binary files should not be treated as ordinary text merely to eliminate warnings.

A project may want shell scripts checked out with LF while a Windows-specific command file needs CRLF. Express those requirements narrowly and review them with the people using both environments. A global autocrlf setting is a user preference; a committed attributes policy is shared project configuration. Understand both when diagnosing inconsistent results.

Inspect and propose targeted rules

The sample is a starting point, not a universal policy. Add only rules justified by the repository’s file types. Use check-attr on representative paths and ls-files --eol to inspect index and working-copy line-ending information. These checks help explain behavior before you perform any conversion.

  1. Review the existing attributes and configuration before adding conflicting rules. More specific directory rules may affect the path you are testing.
  2. If existing tracked files need normalization, plan a dedicated, reviewed change from a clean or safely preserved state rather than mixing it with a behavior fix.
shell
# Example .gitattributes policy
* text=auto
*.sh text eol=lf
*.cmd text eol=crlf

# Inspect representative files
git check-attr text eol -- scripts/check.sh
git ls-files --eol -- scripts/check.sh

Keep normalization reviewable

A normalization commit can touch many lines without changing visible words. Reviewers need a clear explanation of why those files changed, which policy caused it, and how you checked that binary or generated files were not damaged. Run relevant build and script checks on the platforms the policy is meant to support.

Do not use an enormous whitespace diff as evidence that every file needs rewriting. An editor setting, a local conversion rule, or an encoding change may be the actual cause. Inspect one representative file first. After adopting the policy, verify a fresh checkout or the project’s established cross-platform validation procedure, then document exceptions where tools require them. The aim is predictable collaboration, not forcing every file extension into one line-ending style regardless of how it is consumed.

Reviewers should be able to distinguish the policy file from its mechanical consequences. Keeping those changes clearly described makes future line-ending investigations much easier across operating systems.

Sources and further reading

AI-assisted writing with source-linked guidance and illustrative examples. Read our editorial approach or report a correction.

All git fundamentals guides →

Read as MarkdownAll guides

Set a shared line-ending policy with .gitattributes · gitHtml