Skip to content

Instantly share code, notes, and snippets.

@DrEsteban
Last active June 21, 2024 00:02
Show Gist options
  • Save DrEsteban/05b1abbdb199c3c1b9525e12069ab3d7 to your computer and use it in GitHub Desktop.
Save DrEsteban/05b1abbdb199c3c1b9525e12069ab3d7 to your computer and use it in GitHub Desktop.
PSA: *Always* add a .gitignore to your repos! Cross-platform developers will thank you.
* text=auto
# Make sure bash scripts get checked out with correct line endings
*.sh text eol=lf

PSA: Always add a .gitattributes file to your repos!

I know, I know... It's no longer "cool" to develop on Windows. Many developers these days have switched to M1 Mac and not looked back. And to be honest, I don't blame them. Apple's M-series chips are incredibly impressive.

But it has caused developers to forget/get lazy about a critical consideration - line endings!!

Most tools & languages these days have gotten a lot less picky about LF vs CRLF, and will happily compile either. I bet most developers have even forgotten line endings are even a thing!

There's one big holdout, though... bash/sh

Have you ever checked out a repo on your Windows dev machine, noticed it has a .sh script file, popped open your WSL distro to run it, and been presented with:

/> ./run.sh
-bash: ./run.sh: /bin/bash^M: bad interpreter: No such file or directory

...or worse yet:

/> ./run.sh
./run.sh: line 1: $'\r': command not found
./run.sh: line 4: $'\r': command not found

This is because the maintainer of that repository got lazy, forgot about, or has simple hatred for, Window users and line endings!! If unspecified, git will "helpfully" try to check files out in a format that makes sense for the platform. On Windows, the convention is CRLF line endings. Which, as stated before, isn't as big of a deal these days... Except when it is 🙂

So, do your fellow Windows-using comrades a favor...and ADD A .GITATTRIBUTES FILE TO YOUR REPOS - one that at least specifies *.sh with eol=lf, and anything else you're aware of that would affect out-of-the-box cross platform functionality.

Sincerely,
-DrEsteban

# AFTER COMMITTING A .GITATTRIBUTES FILE...
# Run this command to ensure checked-in versions of your files are respecting the new rules
git add --renormalize .
# Commit any changes
# Run these commands to ensure your local checkout is respecting the new settings with:
git rm -rf --cached .
git reset --hard HEAD
# (shouldn't generate any changes in git)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment