防止在 GitHub 上推送到 master?
Prevent pushing to master on GitHub?
GitHub 允许您配置存储库以便 users can't force push to master,但是有没有办法完全阻止推送到 master?我希望做到这一点,以便将提交添加到 master 的唯一方法是通过 GitHub 拉取请求 UI.
您可以 enable branch restrictions 并决定允许谁(在组织的用户和团队方面)推送。
https://help.github.com/articles/about-branch-restrictions/
«注意:如果 "Include administrators" 被选中并且您已经在分支上启用了必需的状态检查并且它们失败了,那么任何将更改推送到基本分支的尝试也将失败,无论用户或团队的权限状态。»
当启用 status checks 时,直接推送到远程主机被拒绝,这意味着在远程主机上添加提交的唯一方法是在 GitHub 上合并拉取请求(通过状态检查)。
这是我对需要状态检查的主分支的实验结果:
- 在我的 PC 上的 master 分支上创建一个提交。
- 推送到远程主机。
- 出现一条拒绝信息。提交最终没有被推送到远程。
C:\GitRepo\GitHub\TomoyukiAota\photo-location-map [master ↑1]> git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 305 bytes | 305.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
remote: error: GH006: Protected branch update failed for refs/heads/master.
remote: error: 3 of 3 required status checks are expected.
To https://github.com/TomoyukiAota/photo-location-map.git
! [remote rejected] master -> master (protected branch hook declined)
error: failed to push some refs to 'https://github.com/TomoyukiAota/photo-location-map.git'
C:\GitRepo\GitHub\TomoyukiAota\photo-location-map [master ↑1]>
自最初的问题/答案以来,Github 已为此在受限分支 UI 添加了一个新选项,允许您进行设置。
Require pull request reviews before merging When enabled, all commits must be made to a non-protected branch and submitted via a
pull request with the required number of approving reviews and no
changes requested before it can be merged into a branch that matches
this rule.
要找到它,请转到“设置”>“分支”>“分支保护规则”
然后点击 'Add Rule'。
然后,输入您要保护的分支的名称,并单击复选框以在合并前要求进行拉取请求审查。
默认情况下,这只会阻止不是版主的人。后面还有另一个复选框,用于确保即使是版主也不能合并。
当前接受的答案实际上是正确的,但如果您是组织所有者或拥有管理员权限(如果您创建了存储库就是这种情况),您仍然可以推送到受保护的分支。来自 https://help.github.com/en/articles/about-branch-restrictions 的 Github 文档:
Organization owners and people with admin permissions for a repository are always able to push to a protected branch.
对于任何其他类型的协作者,git push
将失败。
如果你真的想完全禁用推送,那么你必须通过为分支配置无效的 pushRemote
在本地进行设置,如前所述:
git config branch.master.pushRemote no_push
或者通过创建一个预推送挂钩,如下所示:https://gist.github.com/vlucas/8009a5edadf8d0ff7430
如果您使用的是 Node,则可以使用 husky 创建推送前验证,以确保不会发生直接推送到 master。这样,您仍然可以使用您的管理员权限来合并 PR。我猜其他语言也有类似 husky 的解决方案。
npm install husky --save-dev
- 在
/.huskyrc.js
中:
const preventPushToMaster = `branch=\`git symbolic-ref HEAD\`
if [ "$branch" = "refs/heads/master" ]; then
echo "\033[31mDirect push to master is not allowed.\033[0m"
exit 1
fi`;
module.exports = {
hooks: {
'pre-push': preventPushToMaster,
},
};
如果您在 Github 的私人仓库上使用 free-plan,您可能无法使用受保护的分支功能。所以你需要阻止来自本地的任何推送/提交。
这就是我为了让它在本地工作并分发给所有 repo 成员所做的。
首先需要安装husky来控制pre-commit和pre-push钩子。
然后,我制作了一个 pre-push bash 脚本并将其提交到存储库中。然后使用 husky 参数从 husky pre-push 钩子调用此脚本。
这是我在里面的哈士奇配置package.json
(可以单独配置)
"husky": {
"hooks": {
"pre-commit": "./commands/pre-commit",
"pre-push": "./commands/pre-push $HUSKY_GIT_STDIN"
}
},
如您所见,我有 2 个脚本,一个用于 pre-push,一个用于 pre-commit。
这是我的 commands/pre-push
bash 脚本
#!/bin/bash
echo -e "===\n>> Talenavi Pre-push Hook: Checking branch name / Mengecek nama branch..."
BRANCH=`git rev-parse --abbrev-ref HEAD`
PROTECTED_BRANCHES="^(master|develop)"
if [[ != *"$BRANCH"* ]]
then
echo -e "\n You must use (git push origin $BRANCH) / Anda harus menggunakan (git push origin $BRANCH).\n" && exit 1
fi
if [[ "$BRANCH" =~ $PROTECTED_BRANCHES ]]
then
echo -e "\n Cannot push to remote $BRANCH branch, please create your own branch and use PR."
echo -e " Tidak bisa push ke remote branch $BRANCH, silahkan buat branch kamu sendiri dan gunakan pull request.\n" && exit 1
fi
echo -e ">> Finish checking branch name / Selesai mengecek nama branch.\n==="
exit 0
脚本基本上会做两件事:
- 这个脚本会阻止任何试图推送到某个分支的人(在我的例子中,我不希望任何人——包括我自己——直接推送到
master
和 develop
分支)。他们需要在自己的分支中工作,然后创建拉取请求。
- 此脚本将阻止任何试图推送到与其当前活动分支不同的分支的人。例如,您在分支
fix/someissue
中,但随后您错误地键入了 git push origin master
.
有关更详细的说明,您可以参考这篇文章:
https://github.com/talenavi/husky-precommit-prepush-githooks
I'm hoping to make it so that the only way of adding to commits to master is through the GitHub pull request UI.
我有一个解决方案可以防止推送到主分支,并且不需要批准或长时间的状态检查来传递拉取请求。
诀窍是创建一个立即通过的状态检查。
在 .github/workflows/requirePullRequest.yml
中创建以下 GitHub 操作。
name: require pull request
on:
pull_request:
branches:
- master
jobs:
job:
name: require pull request
runs-on: ubuntu-latest
steps:
- run: echo hello
接下来,更新存储库设置以要求 require pull request
状态检查通过。
如果您希望管理员遵循相同的规则,则必须检查 include administrators
规则。
这样,GitHub 将拒绝所有直接推送到主分支,并且拉取请求不会被任何延迟。
我需要避免不小心将任何提交推送到 master 分支。
它与@christian-chandra 的解决方案类似,但更简单。
转到您的本地工作副本并
$ cd .git/hooks
echo '' > pre-commit
$ chmod +x pre-commit
将此内容添加到文件中(pre-commit)
#!/bin/sh
branch="$(git rev-parse --abbrev-ref HEAD)"
if [ "$branch" = "master" ]; then
echo "Master Branch commit is blocked"
exit 1
fi
完成!
GitHub 允许您配置存储库以便 users can't force push to master,但是有没有办法完全阻止推送到 master?我希望做到这一点,以便将提交添加到 master 的唯一方法是通过 GitHub 拉取请求 UI.
您可以 enable branch restrictions 并决定允许谁(在组织的用户和团队方面)推送。
https://help.github.com/articles/about-branch-restrictions/
«注意:如果 "Include administrators" 被选中并且您已经在分支上启用了必需的状态检查并且它们失败了,那么任何将更改推送到基本分支的尝试也将失败,无论用户或团队的权限状态。»
当启用 status checks 时,直接推送到远程主机被拒绝,这意味着在远程主机上添加提交的唯一方法是在 GitHub 上合并拉取请求(通过状态检查)。
这是我对需要状态检查的主分支的实验结果:
- 在我的 PC 上的 master 分支上创建一个提交。
- 推送到远程主机。
- 出现一条拒绝信息。提交最终没有被推送到远程。
C:\GitRepo\GitHub\TomoyukiAota\photo-location-map [master ↑1]> git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 305 bytes | 305.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
remote: error: GH006: Protected branch update failed for refs/heads/master.
remote: error: 3 of 3 required status checks are expected.
To https://github.com/TomoyukiAota/photo-location-map.git
! [remote rejected] master -> master (protected branch hook declined)
error: failed to push some refs to 'https://github.com/TomoyukiAota/photo-location-map.git'
C:\GitRepo\GitHub\TomoyukiAota\photo-location-map [master ↑1]>
自最初的问题/答案以来,Github 已为此在受限分支 UI 添加了一个新选项,允许您进行设置。
Require pull request reviews before merging When enabled, all commits must be made to a non-protected branch and submitted via a pull request with the required number of approving reviews and no changes requested before it can be merged into a branch that matches this rule.
要找到它,请转到“设置”>“分支”>“分支保护规则”
然后点击 'Add Rule'。
当前接受的答案实际上是正确的,但如果您是组织所有者或拥有管理员权限(如果您创建了存储库就是这种情况),您仍然可以推送到受保护的分支。来自 https://help.github.com/en/articles/about-branch-restrictions 的 Github 文档:
Organization owners and people with admin permissions for a repository are always able to push to a protected branch.
对于任何其他类型的协作者,git push
将失败。
如果你真的想完全禁用推送,那么你必须通过为分支配置无效的 pushRemote
在本地进行设置,如前所述:
git config branch.master.pushRemote no_push
或者通过创建一个预推送挂钩,如下所示:https://gist.github.com/vlucas/8009a5edadf8d0ff7430
如果您使用的是 Node,则可以使用 husky 创建推送前验证,以确保不会发生直接推送到 master。这样,您仍然可以使用您的管理员权限来合并 PR。我猜其他语言也有类似 husky 的解决方案。
npm install husky --save-dev
- 在
/.huskyrc.js
中:
const preventPushToMaster = `branch=\`git symbolic-ref HEAD\`
if [ "$branch" = "refs/heads/master" ]; then
echo "\033[31mDirect push to master is not allowed.\033[0m"
exit 1
fi`;
module.exports = {
hooks: {
'pre-push': preventPushToMaster,
},
};
如果您在 Github 的私人仓库上使用 free-plan,您可能无法使用受保护的分支功能。所以你需要阻止来自本地的任何推送/提交。
这就是我为了让它在本地工作并分发给所有 repo 成员所做的。
首先需要安装husky来控制pre-commit和pre-push钩子。 然后,我制作了一个 pre-push bash 脚本并将其提交到存储库中。然后使用 husky 参数从 husky pre-push 钩子调用此脚本。
这是我在里面的哈士奇配置package.json
(可以单独配置)
"husky": {
"hooks": {
"pre-commit": "./commands/pre-commit",
"pre-push": "./commands/pre-push $HUSKY_GIT_STDIN"
}
},
如您所见,我有 2 个脚本,一个用于 pre-push,一个用于 pre-commit。
这是我的 commands/pre-push
bash 脚本
#!/bin/bash
echo -e "===\n>> Talenavi Pre-push Hook: Checking branch name / Mengecek nama branch..."
BRANCH=`git rev-parse --abbrev-ref HEAD`
PROTECTED_BRANCHES="^(master|develop)"
if [[ != *"$BRANCH"* ]]
then
echo -e "\n You must use (git push origin $BRANCH) / Anda harus menggunakan (git push origin $BRANCH).\n" && exit 1
fi
if [[ "$BRANCH" =~ $PROTECTED_BRANCHES ]]
then
echo -e "\n Cannot push to remote $BRANCH branch, please create your own branch and use PR."
echo -e " Tidak bisa push ke remote branch $BRANCH, silahkan buat branch kamu sendiri dan gunakan pull request.\n" && exit 1
fi
echo -e ">> Finish checking branch name / Selesai mengecek nama branch.\n==="
exit 0
脚本基本上会做两件事:
- 这个脚本会阻止任何试图推送到某个分支的人(在我的例子中,我不希望任何人——包括我自己——直接推送到
master
和develop
分支)。他们需要在自己的分支中工作,然后创建拉取请求。 - 此脚本将阻止任何试图推送到与其当前活动分支不同的分支的人。例如,您在分支
fix/someissue
中,但随后您错误地键入了git push origin master
.
有关更详细的说明,您可以参考这篇文章:
https://github.com/talenavi/husky-precommit-prepush-githooks
I'm hoping to make it so that the only way of adding to commits to master is through the GitHub pull request UI.
我有一个解决方案可以防止推送到主分支,并且不需要批准或长时间的状态检查来传递拉取请求。
诀窍是创建一个立即通过的状态检查。
在 .github/workflows/requirePullRequest.yml
中创建以下 GitHub 操作。
name: require pull request
on:
pull_request:
branches:
- master
jobs:
job:
name: require pull request
runs-on: ubuntu-latest
steps:
- run: echo hello
接下来,更新存储库设置以要求 require pull request
状态检查通过。
如果您希望管理员遵循相同的规则,则必须检查 include administrators
规则。
这样,GitHub 将拒绝所有直接推送到主分支,并且拉取请求不会被任何延迟。
我需要避免不小心将任何提交推送到 master 分支。 它与@christian-chandra 的解决方案类似,但更简单。
转到您的本地工作副本并
$ cd .git/hooks
echo '' > pre-commit
$ chmod +x pre-commit
将此内容添加到文件中(pre-commit)
#!/bin/sh
branch="$(git rev-parse --abbrev-ref HEAD)"
if [ "$branch" = "master" ]; then
echo "Master Branch commit is blocked"
exit 1
fi
完成!