2011-10-27 2 views
9

실수로 master 브랜치에 뭔가 커밋을하는 것을 멈추고 싶습니다. 그래서이 스크립트를 사용하여 어떤 브랜치인지 확인했지만 문제가 있습니다. 새 브랜치를 만들 때 이름 브랜치는 다른 브랜치에 있지만 마스터를 반환합니다.git hook pre-commit을 사용하여 master에게 커밋을 중지하는 방법

$ git branch 
    ignore 
    master 
* set_support 
$ git name-rev --name-only HEAD 
master 

이것은 내 스크립트입니다.

#!/bin/sh 
# Check to see if we are on master branch. Stop accidental commits 
if [ "`git name-rev --name-only HEAD`" == "master" ] 
then 
    if [ -f i_want_to_commit_to_master ] 
    then 
     rm i_want_to_commit_to_master 
     exit 0 
    else 
     echo "Cannot commit to master branch Adrian" 
     echo "Remember to create file 'touch i_want_to_commit_to_master' to commit to master" 
    fi 
    exit 1 
fi 
exit 0 

마크 : 최신 안정 태그와 동일한 결과에 대해 git를 다시 작성했습니다. 새 분기를 완결 한 후에 만 ​​작동합니다.

$ mkdir gittest 
$ cd gittest 
$ git init 
Initialized empty Git repository in /home/adrian/gittest/.git/ 
$ touch file1 
$ git add file1 
$ git commit 
[master (root-commit) 7c56424] New file 
0 files changed, 0 insertions(+), 0 deletions(-) 
create mode 100644 file1 
$ git branch 
* master 
$ git checkout -b new_branch 
Switched to a new branch 'new_branch' 
$ git name-rev --name-only HEAD 
master 
$ git --version 
git version 1.7.7.1 
$ git branch 
    master 
* new_branch 
$ touch file2 
$ git add file2 
$ git commit 
[new_branch 1e038fb] new file 
0 files changed, 0 insertions(+), 0 deletions(-) 
create mode 100644 file2 
$ git name-rev --name-only HEAD 
new_branch 
+0

사용중인 git의 버전 및 운영 체제는 무엇입니까? 'git branch'의 결과와'git name-rev HEAD '의 결과는 정확하게 복사하고 정확하게 붙인 경우 (놀라운 버그)처럼 보입니다. –

+0

나는 소스에서 자식을 구축 - 지난 빌드가 $의 자식이 2.6.40.3-0.fc15 iceweasel.bluedreamer v1.7.7-RC3 $ 자식 --version 자식 버전 1.7.7-RC3 $ 끝나면 uname -a 리눅스를 설명했다 .x86_64 # 1 SMP Tue Aug 16 04:10:59 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux –

답변

11

이 명령은 커밋의 친숙한 이름을 찾는 데 사용됩니다. 일어나는 일은 HEAD가 커밋의 sha1을 먼저 해결하고 이름이 결정된다는 것입니다. 나는 어떤 이름으로 master를 고르는 것 같아요. git log --decorate이 올 것입니다.

난 그냥 테스트에 git branch의 출력을 구문 분석 :

"`git branch | grep \* | cut -f2 -d' '` == "master" 

또는 더 직접적인 방법은 다음과 같습니다

$(git symbolic-ref HEAD 2>/dev/null) == "refs/heads/master" 
+1

아담 (Adam) - 나는 약간의 변화를 만들었지 만 현재는 "$ (git symbolic-ref HEAD 2/dev/null) "=="refs/heads/master "] –

+0

니스 .. 나는 다른 사람들을 위해서 대답을 편집 할 것이다. –

8

this answer에서 제안대로 git rev-parse를 사용할 수있는 대안으로. if 표현식은 다음과 같습니다.

"$(git rev-parse --abbrev-ref HEAD)" == "master" 
관련 문제