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

前端 未结 4 2022
隐瞒了意图╮
隐瞒了意图╮ 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:51

    The post-checkout hook receives three parameters:

    1. Ref of previous HEAD
    2. Ref of new HEAD
    3. Whether this is a file checkout (0) or branch checkout (1)

    You can use the fact that a branch created from the current HEAD will have the same value for parameters 1 and 2.

    cat > .git/hooks/post-checkout <<"EOF"
    if [ "$3" == "0" ]; then exit; fi
    if [ "$1" == "$2" ]; then 
      echo "New branch created. (Probably)."
    fi
    EOF
    
    chmod u+x .git/hooks/post-checkout
    

    Limitations:

    • Checking out an existing branch which happens to be at the same HEAD as the current HEAD will fool it.
    • Creating a new branch not from the current HEAD will not be detected.

提交回复
热议问题