협업 규칙
3. 코드 예제
예제 1: .gitattributes 로 줄 끝 정규화 — 01_team_rules.sh 발췌
mkdir -p src
printf 'first line\r\nsecond line\r\n' > src/Notes.txt
printf '@echo off\r\necho build\r\n' > build.bat
cat > .gitattributes <<'EOF'
* text=auto eol=lf
*.bat eol=crlf
*.png -text
EOF
git add .gitattributes src/Notes.txt build.bat
git ls-files --eolwarning: in the working copy of 'src/Notes.txt', CRLF will be replaced by LF the next time Git touches it
i/lf w/lf attr/text=auto eol=lf .gitattributes
i/lf w/crlf attr/text=auto eol=crlf build.bat
i/lf w/crlf attr/text=auto eol=lf src/Notes.txtCRLF 로 만든 Notes.txt 를 git add 하자 저장소 인덱스(i/)는 이미 lf 로 기록됩니다. build.bat 만 eol=crlf 예외가 적용된 것을 attr/ 칸에서 확인할 수 있습니다.
예제 2: commit-msg 훅 — 형식 위반 거부와 통과 — 01_team_rules.sh 발췌
echo "hello" > app.txt
git add app.txt
git commit -m "파일 수정함"
echo "(종료 코드 $?)"
commit_at "fix: app.txt 문구 수정"
git log --oneline -1commit-msg: 형식 위반입니다. 예) feat: 로그인 화면 추가
(종료 코드 1)
[main 9d03e42] fix: app.txt 문구 수정
1 file changed, 1 insertion(+)
create mode 100644 app.txt
9d03e42 fix: app.txt 문구 수정"파일 수정함" 은 feat|fix|docs|refactor|chore: 내용 패턴이 아니라서 훅이 커밋 자체를 막습니다. fix: app.txt 문구 수정 은 패턴을 통과해 정상 커밋됩니다.
예제 3: pre-commit 훅 — 금지 파일과 큰 파일 거부 — 01_team_rules.sh 발췌
echo "dummy bytecode" > Foo.class
git add Foo.class
git commit -m "chore: Foo.class 추가"
echo "(종료 코드 $?)"
git reset -q Foo.class
head -c 2097152 /dev/zero > big.bin
git add big.bin
git commit -m "chore: big.bin 추가"
echo "(종료 코드 $?)"pre-commit: 금지 파일 Foo.class (컴파일 산출물은 커밋하지 않습니다)
(종료 코드 1)
pre-commit: 큰 파일 big.bin (2097152 bytes, 1MB 초과)
(종료 코드 1)메시지 형식은 맞지만 .class 파일과 1MB 넘는 big.bin 은 pre-commit 훅이 파일 목록을 검사해 거부합니다. git reset 으로 스테이징만 취소하고 다음 시도로 넘어갑니다.
예제 4: pre-push 훅 — main 직접 push 차단 — 01_team_rules.sh 발췌
git push origin main
echo "(종료 코드 $?)"
git switch -q -c feature/42-notice
git push origin feature/42-notice
echo "(종료 코드 $?)"pre-push: main 직접 push 는 금지입니다. 브랜치를 만들어 PR 로 병합하세요.
error: failed to push some refs to 'WORK/origin.git'
(종료 코드 1)
To WORK/origin.git
* [new branch] feature/42-notice -> feature/42-notice
(종료 코드 0)pre-push 훅은 push 하려는 원격 참조가 refs/heads/main 인지만 봅니다. 같은 커밋이라도 feature 브랜치로 push 하면 통과합니다.