#git 브랜치 이름으로 커밋 메시지 만들기

1 minute read

Template

#12345 message

회사에서 사용 중인 커밋 메시지 규칙. 숫자는 일감 번호이다. # 문자를 사용하기 때문에 git에서 commentchar를 $ 문자로 세팅해서 사용 중이다.

$ git co -b 12345 master

항상 로컬 브랜치에서 작업한다. 브랜치 이름은 일감 번호. 커밋을 할 때마다 #12345 문자열을 제일 앞에 처넣는데, 여간 불편한 게 아니다. master가 아닌 브랜치에서 커밋할 때에는 알아서 붙여주면 좋겠다. 그래서 작업 고고.

prepare-commit-msg 훅을 사용하면 된다. 걍 추가하려니 고려해야 할 게 많다. commit, commit -m, commit –amend 명령을 구분해야 한다. 귀찮다 귀찮아.

커밋 템플릿을 사용하면 귀찮은 거 안 할 수 있겠다. 커밋할 때, git이 에디터로 수정하는 파일에 넣어주는 템플릿이다. -m, --amend 이런 옵션을 사용할 때, 템플릿을 사용 안 하니 따로 처리해줄 필요 없다. 여기에 업혀가면 되겠다. 업힐 수 있을 때, 업히자.

$ cat ~/.dotfiles/git/commit_template_issue_branch
${issue}

$ git config commit.template ~/.dotfiles/git/commit_template_issue_branch

[issue] 문자열은 prepare-commit-msg 훅에서 브랜치 이름으로 바꿔친다.

$ cat .git/hooks/prepare-commit-msg
#!/bin/sh

branchname=`git rev-parse --abbrev-ref HEAD`
issuetag="\${issue}"
if [ "$branchname" = "master" ]; then

    sed -i "s/$issuetag//" "$1"

else

    sed -i "s/$issuetag/\#$branchname/" "$1"

fi

master 브랜치일 경우엔 일감 번호를 없앤다. 브랜치 이름이 숫자일 경우에만 붙일까? 됐다. 나중에 그렇게 사용할 일 있으면 수정하면 된다.

$ cat ~/.dotfiles/.gitattributes | grep eol
commit_template_issue_branch text eol=lf

windows에서 사용 중인데, 커밋 메시지 템플릿은 무조건 LF 라인 엔딩이어야 한다. 그냥 저장해서 사용하니 emacs에서 ^M 문자가 계속 나온다. 이 파일은 플랫폼에 상관없이 LF 라인 엔딩을 사용하게 수정. git 버전은 2.6.2

Updated <2016-03-22 Tue> 태그를 [issue]에서 ${issue}로 변경