「Git」:修訂間差異

出自Gea-Suan Lin's Wiki
跳至導覽 跳至搜尋
Gslin留言 | 貢獻
Gslin留言 | 貢獻
第5行: 第5行:
可以設計一些hook在本地端擋住常見的錯誤行為。
可以設計一些hook在本地端擋住常見的錯誤行為。


禁止<code>develop</code>從其他branch rebase更新,但允許<code>origin/develop</code>,放在<code>~/.git/hooks/pre-rebase</code>:
禁止在<code>develop</code>下直接commit,放在<code>.git/hooks/pre-commit</code>:
 
<syntaxhighlight lang="bash">
#!/bin/bash
 
if [[ "$(git rev-parse --abbrev-ref HEAD)" = "develop" ]]; then
    echo "You cannot commit directly to the develop branch."
    exit 1
fi
</syntaxhighlight>
 
禁止<code>develop</code>從其他branch rebase更新,但允許<code>origin/develop</code>,放在<code>.git/hooks/pre-rebase</code>:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">

於 2024年12月23日 (一) 08:35 的修訂

Git是一套版本控制系統(VCS)。

Hook

可以設計一些hook在本地端擋住常見的錯誤行為。

禁止在develop下直接commit,放在.git/hooks/pre-commit

#!/bin/bash

if [[ "$(git rev-parse --abbrev-ref HEAD)" = "develop" ]]; then
    echo "You cannot commit directly to the develop branch."
    exit 1
fi

禁止develop從其他branch rebase更新,但允許origin/develop,放在.git/hooks/pre-rebase

#!/bin/bash

# $1: source branch.
# $2: destination branch (empty if it's current branch).

if [[ "$2" = "develop" || ( "$2" = "" && "$(git rev-parse --abbrev-ref HEAD)" = "develop" ) ]]; then
    if [[ "$1" = "origin/develop" ]]; then
        # OK
        exit 0
    fi

    echo "You cannot rebase develop branch from branch other than origin/develop."
    exit 1
fi

外部連結