限制推送到分支的某些遥控器
Restrict push to certain remotes for a branch
我有两台遥控器,一台用于生产,一台用于 GitHub。由于我必须将某些文件推送到不得登陆 GitHub 的作品,因此我想限制一个分支以避免发生意外。
有没有办法告诉我的 git 客户端它应该 从不 将分支 "deploy" 推送到远程 "github" 并且只推送这个分支到 "production"?
Git Hooks are what you're looking for. You can set up a server side pre-receive hook, that works for everyone trying to push to your repo, that accepts or rejects pushes depending on the current branch, or a similar local pre-push hook. Examples for the latter can be found here and here.
是正确的,前提是您的 Git 至少是 1.8.2(如果不是,您应该升级 :-))。但是,链接的示例是错误的。
使用两个参数调用 pre-push hook,它们提供:
- 遥控器的名称,如果使用命名遥控器,则为 URL
- URL(扩展命名遥控器的结果,如果使用命名遥控器)
什么是被推送,这是你在运行git push
时指定的东西,提供给钩子在其标准输入上。这是两个链接示例都损坏的地方。
如果你运行:
git push github refspec1 refspec2 ... refspecN
那么被推送的引用就是在这个命令行上给出的引用。
如果你运行:
git push github
(没有 refspecs),要推送的分支集是......好吧,它很复杂,但在 modern Git 中,它默认为当前分支。
示例预推送挂钩假设 仅 当前分支将被推送。由于这是现代默认设置,示例可能会起作用,直到您不小心 运行:
git push github deploy
(比如,master
),然后他们就不会了,你可能会很难过。 :-)
要修复它们,请使用读取的 githook,例如:
#! /bin/sh
[ "" = github ] || exit 0 # allow if not pushing to github
while read lref lhash rref rhash; do
case "$lref" in
refs/heads/deploy)
echo "error: attempt to push 'deploy' branch to github" 1>&2
exit 1;;
esac
done
这将允许:
git push github master:deploy
(在远程 github
上创建或更新 deploy
,但使用 local 分支 master
,而不是 local branch deploy
) while forbidding:
git push github deploy:oops
(将 local deploy
推送到名为 oops
的分支)。
如果您希望有更复杂的规则,请将它们写下来。注意,如果要防止使用git push https://github.com/...
绕过自己的hook,可以勾选</code>和<code>
。当然,如果你决定绕过你自己的钩子,你可以很容易地 运行 git push --no-verify
禁用你的钩子。
我有两台遥控器,一台用于生产,一台用于 GitHub。由于我必须将某些文件推送到不得登陆 GitHub 的作品,因此我想限制一个分支以避免发生意外。
有没有办法告诉我的 git 客户端它应该 从不 将分支 "deploy" 推送到远程 "github" 并且只推送这个分支到 "production"?
Git Hooks are what you're looking for. You can set up a server side pre-receive hook, that works for everyone trying to push to your repo, that accepts or rejects pushes depending on the current branch, or a similar local pre-push hook. Examples for the latter can be found here and here.
使用两个参数调用 pre-push hook,它们提供:
- 遥控器的名称,如果使用命名遥控器,则为 URL
- URL(扩展命名遥控器的结果,如果使用命名遥控器)
什么是被推送,这是你在运行git push
时指定的东西,提供给钩子在其标准输入上。这是两个链接示例都损坏的地方。
如果你运行:
git push github refspec1 refspec2 ... refspecN
那么被推送的引用就是在这个命令行上给出的引用。
如果你运行:
git push github
(没有 refspecs),要推送的分支集是......好吧,它很复杂,但在 modern Git 中,它默认为当前分支。
示例预推送挂钩假设 仅 当前分支将被推送。由于这是现代默认设置,示例可能会起作用,直到您不小心 运行:
git push github deploy
(比如,master
),然后他们就不会了,你可能会很难过。 :-)
要修复它们,请使用读取的 githook,例如:
#! /bin/sh
[ "" = github ] || exit 0 # allow if not pushing to github
while read lref lhash rref rhash; do
case "$lref" in
refs/heads/deploy)
echo "error: attempt to push 'deploy' branch to github" 1>&2
exit 1;;
esac
done
这将允许:
git push github master:deploy
(在远程 github
上创建或更新 deploy
,但使用 local 分支 master
,而不是 local branch deploy
) while forbidding:
git push github deploy:oops
(将 local deploy
推送到名为 oops
的分支)。
如果您希望有更复杂的规则,请将它们写下来。注意,如果要防止使用git push https://github.com/...
绕过自己的hook,可以勾选</code>和<code>
。当然,如果你决定绕过你自己的钩子,你可以很容易地 运行 git push --no-verify
禁用你的钩子。