AI Hints
Home›Git & GitHub Tutorial›Everyday Gotchas
Git & GitHub · Lesson 115 of 144

Line Endings

Windows, Mac and Linux.

⏱ 2 min read▶ Video at 2:06:11🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

Here's a strange problem you might meet in a mixed team. You open a file, change nothing, and Git says every line has changed. The cause is invisible. Line endings.

Line Endings
From the video: Line Endings
  • Changed nothing?
  • Git says every line changed
  • The cause: line endings

Two kinds of line ending

At the end of every line in a text file, there's an invisible character. Windows uses two, called carriage return and line feed. Mac and Linux use just one, line feed. If editors on different computers switch between them, every line looks different to Git.

Windows

  • CRLF
  • Two invisible characters

Mac and Linux

  • LF
  • One invisible character

The fix: a .gitattributes file

The best fix is a file called dot git attributes, committed to the repository. This line says, star, text equals auto. Git then stores every text file with simple line feeds, and gives each person the right endings for their system. Commit it, and the whole team is protected.

git
echo '* text=auto' > .gitattributes
cat .gitattributes
* text=auto
git add .gitattributes
git commit -m "Normalize line endings"
[main 5cc1d38] Normalize line endings
 1 file changed, 1 insertion(+)
 create mode 100644 .gitattributes

Summary

Line endings differ between Windows and Mac or Linux. A dot git attributes file with star text equals auto fixes it for everyone. Next, empty folders.

Key takeaways
  • CRLF vs LF
  • .gitattributes
  • * text=auto
Next up: Empty Folders — why Git ignores them