Skip to content

Instantly share code, notes, and snippets.

@kuronekomichael
Last active October 27, 2020 08:55
Show Gist options
  • Save kuronekomichael/6710402 to your computer and use it in GitHub Desktop.
Save kuronekomichael/6710402 to your computer and use it in GitHub Desktop.
git clone元のサーバによって、user.nameやuser.emailを切り替えるフックスクリプト git clone hook post checkout
#!/bin/sh
# post-checkout
# =============
# arguments
# $1: ref of the previous HEAD e.g.) 0000000000000000000000000000000000000000
# $2: ref of the new HEAD e.g.) 959224d097072c8a5640fee31bac7325710eada1
# $3: flag = ブランチをチェックアウトした場合=1, ファイルをチェックアウトした場合=0
# `git clone`時だけ処理をしたいので、通常の`git checkout`時には何もしない
if [ "$1" != "0000000000000000000000000000000000000000" -o "$3" != "1" ]; then
exit
fi
GIT_REMOTE=`git remote -v`
echo ""
if [[ "$GIT_REMOTE" =~ "@github.com:" ]]; then
echo "\033[32m ✓ checkout from github.com\033[0m"
git config --local user.name "<username for github>"
git config --local user.email "<email for github>"
elif [[ "$GIT_REMOTE" =~ "@bitbucket.org:" ]]; then
echo "\033[32m ✓ checkout from bitbucket\033[0m"
git config --local user.name "<username for bitbucket>"
git config --local user.email "<email for bitbucket>"
else
echo "\033[31m ✗ unmatched\033[0m"
fi
git config --local -l
@kuronekomichael
Copy link
Author

kuronekomichael commented Apr 22, 2015

事前に実行可能に設定しておくことを忘れずに。

chmod a+x ~/.git_template/hooks/post-checkout

例a). プロジェクト毎にhookを設定する

{project_path}/.git/hooks/post-checkout

例b). ユーザ毎にhookを設定する

mkdir -p ~/.git_template/hooks/
vi ~/.git_template/hooks/post-checkout
git config --global init.templatedir '~/.git_template'

例c). グローバルにhookを設定する

/usr/share/git-core/templates/hooks/post-checkout

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment