What is the purpose of .gitignore vs. .gitattributes?

Last updated 6 July 2026

Both are config files but solve different problems: .gitignore tells Git which untracked files to not track, while .gitattributes tells Git how to handle files it does track (line endings, diff, merge, filters).

  • .gitignore:

    • Lists patterns for files/dirs Git should ignore: build output, node_modules/, logs, secrets.

    • Only affects untracked files: already-tracked files are not ignored.

  • .gitattributes:

    • Assigns behavior per path: text=auto for line-ending normalization, binary to disable diff/merge.

    • Configures diff/merge drivers, export-ignore for archives, and filters like Git LFS.

  • In short: .gitignore controls presence in the repo; .gitattributes controls treatment within the repo.

  • https://medium.com/@pengcheng1222/mastering-git-part-1-understanding-the-roles-of-gitignore-gitattributes-and-gitkeep-0c1558fcdd1a
  • https://dev.to/charan_gutti_cf60c6185074/mastering-gitignore-keep-your-git-repo-clean-and-professional-c3h
  • https://codeboxr.com/gitignore-vs-gitattributes-vs-distignore/
  • https://www.geeksforgeeks.org/git/gitattributes-vs-gitignore-in-git/
  • https://git-scm.com/docs/gitattributes
  • https://git-scm.com/book/ms/v2/Customizing-Git-Git-Attributes
All Git interview questions