2012年5月9日 星期三

GIT post-commit: Backup commits to "remote branch" of remote repo

Configuration:

  1. gitbackup.remotename: the remote used to backup, default "gitbackup"
  2. gitbackup.myname: the name shown before remote branch of remote repo, default is current git directory "$(pwd)"

Installation:
  1. Copy the following post-commit to .git/hooks
  2. Add remote
    git remote add gitbackup XXX@YYY:/ZZZ
  3. Change configuration if needed
  4. It is suggested to create a ssh key to the remote "gitbackup" to login without password.
    Refer to ssh-keygen.
Done!!! git will also push the commit to remote repo of remote branch while committing:
# git commit -m "update" -a
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 797 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)
To xxx@XXX.XXX:~/prj/mygit
f3fe00e..d46f950 HEAD -> 101/101
[101 d46f950] update
1 files changed, 37 insertions(+), 0 deletions(-)



post-commit
#!/bin/sh
curbr=$(git branch | grep \* | cut -c 3-)
remotename=$(git config gitbackup.remotename)
myname=$(git config gitbackup.myname)

if [ -z "${myname}" ] ;then
myname=$(readlink -e $(pwd))
git config gitbackup.myname ${myname}
fi
if [ -z "${remotename}" ] ;then
remotename=gitbackup
git config gitbackup.remotename ${remotename}
fi
if echo ${curbr} | grep \( 1>/dev/null 2>&1 ; then
echo
echo Warning: you are not on any branch currently...
echo
curbr=no_branch
fi
git push ${remotename} +HEAD:refs/remotes/${myname##/}/${curbr}
: Nothing



Push to remote branch of remote repo
git push <remote_name> +<br_name>:refs/remotes/<my_name>/<br_name>



7.3 Customizing Git - Git Hooks
http://git-scm.com/book/ch7-3.html

[Git] 利用 post-receive hook 自動發 Email 給團隊成員
http://josephjiang.com/entry.php?id=346

沒有留言: