[1] 발견한 이슈
.sh 파일이 원래는 LF 상태로 git에 업로드됨
- 다른 컴퓨터에서 풀 받으니 CRLF로 변환됨
- docker desktop에서 한 컨테이너에서 호스트 파일시스템에 볼륨 마운팅 설정
- 이 컨테이너에서 이
.sh 파일을 실행하니 file not found 같은 문제가 발생함
- 원인은
#!/bin/bash 같은 라인의 끝이 CRLF일 때 /bin/bash\\r 파일을 찾으려는 시도를 해서 file not found 문제를 일으키는 것
- 즉, 리눅스 전용 파일들(
.sh 등)은 LF로 사용해야만 함
[2] .gitattribute 파일 작성
# ================================
# Linux/Unix (반드시 LF)
# ================================
*.sh text eol=lf
*.bash text eol=lf
*.zsh text eol=lf
# Docker
Dockerfile text eol=lf
*.dockerfile text eol=lf
.dockerignore text eol=lf
docker-compose*.yml text eol=lf
# Git hooks (확장자 없는 경우가 많음)
.husky/* text eol=lf
# Makefile
Makefile text eol=lf
*.mk text eol=lf
# Python (shebang 사용 시)
*.py text eol=lf
# Node.js
.nvmrc text eol=lf
# ================================
# Windows (반드시 CRLF)
# ================================
*.bat text eol=crlf
*.cmd text eol=crlf
*.ps1 text eol=crlf
*.nsi text eol=crlf
*.nsh text eol=crlf
*.reg text eol=crlf
# Visual Studio
*.sln text eol=crlf
*.vcxproj text eol=crlf
# ================================
# 기본값
# ================================
* text=auto
[3] .gitattributes 규칙 갱신하면서 커밋
- 주의사항: uncommitted change가 있을 경우 reset hard 하면 날아가니 미리 커밋해야 함
git rm --cached -r .
git reset --hard
git add .
git commit -m "Normalize CRLF/LF"
⭐ 커밋된 .gitattributes 기준으로 LF/CRLF 일괄 적용하기
rm --cached 명령어를 통해 인덱스를 제거한다.
reset --hard 명령어를 통해 head의 상태를 불러오면서, 이 때 .gitattributes의 규칙을 적용하며 LF/CRLF도 알맞게 적용된다.
- 이 방법을 통해 git 변경사항 없이 아주 깔끔하게 LF/CRLF를 일괄 적용할 수 있다.
- 참고:
.gitattributes가 원격 저장소에 올라가 있지 않고, 로컬 커밋된 상태여도 적용 기준이 된다.
git rm --cached -r .
git reset --hard