「Git」:修訂間差異
跳至導覽
跳至搜尋
第38行: | 第38行: | ||
=== Global === | === Global === | ||
可以將整包專案設定自定義的<code>hooksPath</code>: | |||
<syntaxhighlight lang="ini"> | |||
[core] | |||
hooksPath = /home/gslin/git/work/hooks | |||
</syntaxhighlight> | |||
除了global以外,可以搭配<code>includeIf</code>只針對某些目錄下的專案使用特定的hook: | |||
<syntaxhighlight lang="ini"> | |||
[includeIf "gitdir:~/git/work"] | |||
path = ~/git/work/.gitconfig | |||
</syntaxhighlight> | |||
== 外部連結 == | == 外部連結 == |
於 2024年12月23日 (一) 08:43 的修訂
Git是一套版本控制系統(VCS)。
Hook
Hook是針對某些行為的操作(通常包括了pre-
以及post-
),可以設計一些pre-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
Global
可以將整包專案設定自定義的hooksPath
:
[core]
hooksPath = /home/gslin/git/work/hooks
除了global以外,可以搭配includeIf
只針對某些目錄下的專案使用特定的hook:
[includeIf "gitdir:~/git/work"]
path = ~/git/work/.gitconfig
外部連結
- 官方網站 {{en}