For the first time, I try to collaborate with my colleagues on git. Because we are afraid of ruining the master branch, I add the pre-receive hook on the remote server. At first I was trying to use the /dev/tty as suggested in
https://dev.ghost.org/prevent-master-push/
, however I realize for the remote server the ssh does not provide the prompt. I decide that the master can only be upated if the change has been submitted to stable_release branch.
2017.03.10 update:
(1) deleting stable_release and master branch is not allowed.
(2) new branch can be added only if it ends with _dev.
The code is the following:
# The "post-receive" script is run after receive-pack has accepted a pack # and the repository has been updated. It is passed arguments in through # stdin in the form # <oldrev> <newrev> <refname> # For example: # aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master # # see contrib/hooks/ for a sample, or uncomment the next line and # rename the file to "post-receive". #. /usr/share/git-core/contrib/hooks/post-receive-email while read oldrev newrev refname do # not push if [ "<img src="https://dingxf.cn/blog/wp-content/ql-cache/quicklatex.com-132a9baa84fad69ac3815df24d0857c2_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="{oldrev:(-4)}" == 0000 ]; then     if [ "_dev" != "" title="Rendered by QuickLaTeX.com" height="19" width="331" style="vertical-align: -5px;"/>{refname:(-4)}" ]; then echo "only add new username_dev branch is allowed" exit 1 fi fi if [ "<img src="https://dingxf.cn/blog/wp-content/ql-cache/quicklatex.com-3e22c1b1818bf1acc68cdf4f742fd3d1_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="{oldrev:(-4)}" != 0000 ]; then     branch=" title="Rendered by QuickLaTeX.com" height="19" width="294" style="vertical-align: -5px;"/>(git rev-parse --symbolic --abbrev-ref <img src="https://dingxf.cn/blog/wp-content/ql-cache/quicklatex.com-4cd4be41074d966df2fa31e6873b0e78_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="refname)     if [ "" title="Rendered by QuickLaTeX.com" height="19" width="107" style="vertical-align: -5px;"/>{newrev:(-4)}" == 0000 ]; then if [ "master" == "<img src="https://dingxf.cn/blog/wp-content/ql-cache/quicklatex.com-fc7832d73c2ac5666786f7add88bb15c_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="branch" ] || [ "stable_release" == "" title="Rendered by QuickLaTeX.com" height="19" width="246" style="vertical-align: -5px;"/>branch" ]; then echo "deleting master branch and stable_release branch is not allowed." exit 1 fi fi if [ "master" == "<img src="https://dingxf.cn/blog/wp-content/ql-cache/quicklatex.com-fc7832d73c2ac5666786f7add88bb15c_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="branch" ] || [ "stable_release" == "" title="Rendered by QuickLaTeX.com" height="19" width="246" style="vertical-align: -5px;"/>branch" ]; then stable_head="<img src="https://dingxf.cn/blog/wp-content/ql-cache/quicklatex.com-c2492814b8eb878897d6fa341adfff40_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="(git show-ref -s stable_release)"       if [ "master" == "" title="Rendered by QuickLaTeX.com" height="19" width="404" style="vertical-align: -5px;"/>branch" ]; then if [ "<img src="https://dingxf.cn/blog/wp-content/ql-cache/quicklatex.com-b5935252b939963dd02df6277f399b1a_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="newrev" != "" title="Rendered by QuickLaTeX.com" height="12" width="102" style="vertical-align: 0px;"/>stable_head" ]; then echo "The stable_release is not updated. push to stable release first" echo "If you are a normal user, don't try to push to master branch. Please work your own dev branch. You can push your dev branch. Follow these instructions(change username to your own):" echo "git checkout -b username_dev" echo "git branch -D master" echo "git checkout master" echo "git checkout username_dev" echo "git push origin username_dev" exit 1 else echo "You are pushing to the master branch. Be careful and be responsible for what you did. Now I give you 7 seconds to regret" for((i=0;i<7;++i)); do printf "." sleep 1 done echo "" exit 0 fi fi exit 0 fi fi exit 0 done