有时候无意的提交代码 push 到本地主/开发分支时, 你有没有想过把头撞到墙上? 一般这种情况下 我们能通过 `git reset HEAD files` 来取消提交 (unstage), 但是, 这还是挺让人不爽的.
还好, git 提供了很多 hooks 挂钩, 这些挂钩可以在事件完成前后做些检查, 其中 pre-push 就用于在推送代码前进行检查, 当脚本返回1时, 那么动作将停止, 而返回0时则表示一切正常可以继续提交.
pre-push 是 BASH脚本, 在每个代码仓库下的 一个隐藏文件夹 .git/hooks 下. 其中有一个 pre-push 的样例:
#!/bin/sh
# An example hook script to verify what is about to be pushed. Called by "git
# push" after it has checked the remote status, but before anything has been
# pushed. If this script exits with a non-zero status nothing will be pushed.
#
# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If pushing without using a named remote those arguments will be equal.
#
# Information about the commits which are being pushed is supplied as lines to
# the standard input in the form:
#
# <local ref> </local><local sha1> <remote ref> </remote><remote sha1>
#
# This sample shows how to prevent push of commits where the log message starts
# with "WIP" (work in progress).
remote="$1"
url="$2"
z40=0000000000000000000000000000000000000000
while read local_ref local_sha remote_ref remote_sha
do
if [ "$local_sha" = $z40 ]
then
# Handle delete
:
else
if [ "$remote_sha" = $z40 ]
then
# New branch, examine all commits
range="$local_sha"
else
# Update to existing branch, examine new commits
range="$remote_sha..$local_sha"
fi
# Check for WIP commit
commit=`git rev-list -n 1 --grep '^WIP' "$range"`
if [ -n "$commit" ]
then
echo >&2 "Found WIP commit in $local_ref, not pushing"
exit 1
fi
fi
done
exit 0
我们可以通过下面的脚本检查 当前是否在主分支上, 主分支可以自定义: 比如 master, origin/develop, develop 等. 以下脚本需要通过命令 chmod +x pre-push 让其可执行. 当当前分支是主分支时, 则返回1, 否则返回0表示正常继续.
#!/bin/bash
# run `chmod +x .git/hooks/pre-push` (We make the hook executable by using the chmod utility.)
# To bypass this hook and force the push, use 'git push --no-verify'
master_branches=('origin/develop' 'master' 'develop')
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
for branch in "${master_branches[@]}"
do
if [ $branch = $current_branch ]; then
echo "You're about to push $branch and you don't want that"
exit 1
fi
done
exit 0
使用例子:
$ git branch
* master
$ git push origin master
You're about to push master and you don't want that
error: failed to push some refs to 'https://github.com/DoctorLai/ACM.git'
由于每个代码仓库中含有一份 pre-push, 您可以通过以下命令把根目录下的 pre-push 给复制到每一个子代码仓库中.
find . -type d -name "hooks" | grep ".git/hooks" | xargs echo cp pre-push
Windows 下的代码仓库应该用的也是 bash 语言编写的 pre-push 脚本, 否则, 以下 windows 批处理可能可以用.
@echo off
REM pre-push.cmd
REM To bypass this hook and force the push, use 'git push --no-verify'
setlocal enabledelayedexpansion
set branches=origin^/develop master develop
for /f "tokens=2" %%i in ('git branch') do (set current=%%i)
for %%j in (%branches%) do (
if /i "%%j"=="%current%" (
echo You're about to push to %%j, you don't want that
exit /b 1
)
)
endlocal
exit /b 0
Git 还有其它的 Hooks 检查, 比如: git 小技巧: 通过pre-commit hook 来防止程序员把其它测试给禁用了
英文: How to Prevent Commiting to master/develop branch by Accidents using Pre-push Hooks in Git?
–EOF (The Ultimate Computing & Technology Blog) —
强烈推荐
- 英国代购-畅购英伦
- TopCashBack 返现 (英国购物必备, 积少成多, 我2年来一共得了3000多英镑)
- Quidco 返现 (也是很不错的英国返现网站, 返现率高)
- 注册就送10美元, 免费使用2个月的 DigitalOcean 云主机(性价比超高, 每月只需5美元)
- 注册就送10美元, 免费使用4个月的 Vultr 云主机(性价比超高, 每月只需2.5美元)
- 注册就送10美元, 免费使用2个月的 阿里 云主机(性价比超高, 每月只需4.5美元)
- 注册就送20美元, 免费使用4个月的 Linode 云主机(性价比超高, 每月只需5美元) (折扣码: PodCastInit2022)
- PlusNet 英国光纤(超快, 超划算! 用户名 doctorlai)
- 刷了美国运通信用卡一年得到的积分 换了 485英镑
- 注册就送50英镑 – 英国最便宜最划算的电气提供商
- 能把比特币莱特币变现的银行卡! 不需要手续费就可以把虚拟货币法币兑换
微信公众号: 小赖子的英国生活和资讯 JustYYUK