Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

Git Tutorial

Git HOME Git Intro Git Install Git Config Git Get Started Git New Files Git Staging Git Commit Git Remove and Move Git Tagging Git Stash Git History Git Diff Git Blame Git Help Git Branch Git Merge Git Workflow Git Best Practices

Git and GitHub

GitHub Get Started Git What is SSH? GitHub Add SSH GitHub Set Remote GitHub Edit Code Pull from GitHub Push to GitHub GitHub Branch Pull Branch from GitHub Push Branch to GitHub GitHub Flow GitHub Pages Git GUI Clients

Git Contribute

GitHub Fork Git Clone from GitHub GitHub Send Pull Request

Git Undo

Git Revert Git Reset Git Clean Git Amend Git Rebase Git Reflog Git Recovery

Git Advanced

Git .gitignore Git .gitattributes Git Large File Storage (LFS) Git Signing Commits/Tags Git Cherrypick & Patch Git Bisect Git Merge Conflicts Git CI/CD Git Hooks Git Submodules Git Worktree Git Remote Advanced

Git and Bitbucket

Bitbucket Get Started Bitbucket Add SSH Bitbucket Set Remote Bitbucket Edit Code Bitbucket Branch Bitbucket Pages Bitbucket Fork Git Clone from Bitbucket Bitbucket Send Pull Request

Git and GitLab

GitLab Get Started GitLab Add SSH GitLab Set Remote GitLab Edit Code GitLab Branch GitLab Pages GitLab Fork Git Clone from GitLab GitLab Send Pull Request

Git Cert

Git Certificate

Git Reference

Git Glossary

Git Exercises

Git Exercises Git Challenges Git Quiz Git Syllabus Git Study Plan

Git .gitattributes


Share

What is .gitattributes?

The .gitattributes file is a special file that tells Git how to handle specific files in your repository.

It controls things like line endings, file types, merge behavior, custom diff tools, and more.

Everyone on your team gets the same settings because this file is versioned with your project.

For more about Git LFS, see the dedicated page.


When to Use .gitattributes

  • To enforce consistent line endings across different operating systems
  • To mark files as binary (so Git doesn't try to merge or change them)
  • To enable Git LFS for large files
  • To set up custom diff or merge tools for special file types
  • To control how files are exported in archives

Create or Edit .gitattributes

  1. Go to the root of your repository (or a subfolder for local rules).
  2. Create or edit the .gitattributes file.
  3. Add rules, one per line, for how Git should treat files.

Example: Force Unix Line Endings for All Text Files

*.txt text eol=lf


Handle Line Endings

Standardize line endings to avoid merge conflicts and broken files across different OSes.

Example: Set LF for Shell Scripts

*.sh text eol=lf

Mark Files as Binary

Tell Git which files are binary (not text).

This prevents Git from trying to merge or change line endings for these files.

Example: Mark PNG Files as Binary

*.png binary

Enable LFS for File Types

Use Git LFS for large files like images or datasets.

This tells Git to use LFS for these files:

Example: Track PSD Files with LFS

*.psd filter=lfs diff=lfs merge=lfs -text

Custom Diff Settings

Tell Git to use a special tool to compare certain file types (like Markdown or Jupyter notebooks):

Example: Custom Diff for Markdown

*.md diff=markdown

Check Attributes

See what attributes are set for a file:

Example: Check Attributes of a File

git check-attr --all README.md
README.md: diff: markdown
Try it Yourself »

Advanced Usage

  • Merge Strategies: Set custom merge drivers for tricky files (like lock files or notebooks).
  • Export-ignore: Exclude files from tar/zip archives created by git archive:

Example: Ignore Files on Export

docs/* export-ignore

Tips & Best Practices

  • Patterns work like .gitignore (wildcards, etc).
  • Put .gitattributes in subfolders for rules that only apply there.
  • Changing .gitattributes won't retroactively fix files already committed. Run git add --renormalize . to apply the new rules to them.
  • Use git check-attr to debug attribute issues.

Note: .gitattributes is versioned with your project, so everyone on your team gets the same settings.




Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.