Is there a way to trigger a hook after a new branch has been checked out in Git?

前端 未结 4 2025
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 02:27

Is there a way to trigger a hook after a new branch has been checked out in Git?

4条回答
  •  庸人自扰
    2020-11-30 02:52

    Similar to others but verifies that the branch has been checked out once.

    #!/bin/bash
    
    # this is a file checkout – do nothing
    if [ "$3" == "0" ]; then exit; fi
    
    BRANCH_NAME=$(git symbolic-ref --short -q HEAD)
    NUM_CHECKOUTS=`git reflog --date=local | grep -o ${BRANCH_NAME} | wc -l`
    
    #if the refs of the previous and new heads are the same 
    #AND the number of checkouts equals one, a new branch has been created
    if [ "$1" == "$2"  ] && [ ${NUM_CHECKOUTS} -eq 1 ]; then
        git push origin ${BRANCH_NAME}
    fi
    

提交回复
热议问题