GitHub允许您配置您的存储库,以便用户无法强制推送到主分支,但是否有一种方法可以完全防止向主分支推送?我希望使其仅通过GitHub拉取请求UI添加提交到主分支。
GitHub允许您配置您的存储库,以便用户无法强制推送到主分支,但是否有一种方法可以完全防止向主分支推送?我希望使其仅通过GitHub拉取请求UI添加提交到主分支。
对于任何其他类型的协作者,“git push”将失败。组织所有者和拥有存储库管理权限的人始终可以推送到受保护的分支。
pushRemote
,如上所述:
git config branch.master.pushRemote no_push
或者创建一个pre-push挂钩,如此处所示:https://gist.github.com/vlucas/8009a5edadf8d0ff7430pushRemote
的方式仅适用于在终端使用 git 命令推送时,如果像我一样使用 IntelliJ 进行推送,则仍然有效。 - ZhengguanLi当启用状态检查时,直接推送到远程主分支会被拒绝,这意味着将提交添加到远程主分支的唯一方法是在 GitHub 上合并通过状态检查的拉取请求。
以下是我进行的需要状态检查的主分支实验结果:
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]>
"husky": {
"hooks": {
"pre-commit": "./commands/pre-commit",
"pre-push": "./commands/pre-push $HUSKY_GIT_STDIN"
}
},
正如您所看到的,我有两个脚本,一个用于 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 [[ $1 != *"$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
。我希望能够让在GitHub拉取请求界面中添加提交成为将改动推送到主分支的唯一方式。
我有一个解决方案,可以防止将更改推送到主分支,而不需要批准或通过拉取请求中的长状态检查。
关键是创建一个立即通过的状态检查。
在.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
接下来,更新存储库设置以要求要求拉取请求
状态检查通过。
如果你希望管理员遵循相同的规则,那么你必须勾选包括管理员
规则。
这样,GitHub将拒绝所有对主分支的直接推送,并且拉取请求不会被任何事情延迟。
我需要避免意外推送到主分支的任何提交。 这是一个类似@christian-chandra的解决方案,但更简单。
进入您的本地工作副本并
$ cd .git/hooks
echo '' > pre-commit
$ chmod +x pre-commit
#!/bin/sh
branch="$(git rev-parse --abbrev-ref HEAD)"
if [ "$branch" = "master" ]; then
echo "Master Branch commit is blocked"
exit 1
fi
完成!
https://help.github.com/articles/about-branch-restrictions/
注意:如果选中了“包括管理员”,并且您已在分支上启用了所需的状态检查,并且它们失败,则无论用户或团队的权限状态如何,任何尝试将更改推送到基本分支的尝试都将失败。
git config branch.master.pushRemote no_push
。 - Nikhita Raghunathnpm 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,
},
};