有没有办法在 GIT 中锁定分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2471340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to lock a branch in GIT
提问by Senthil A Kumar
I have an idea of locking a repository from users pushing files into it by having a lock script in the GIT update hook since the push can only recognize the userid as arguments and not the branches. So i can lock the entire repo which is just locking a directory.
我有一个想法,通过在 GIT 更新挂钩中使用锁定脚本来锁定用户将文件推送到其中的存储库,因为推送只能将用户 ID 识别为参数而不是分支。所以我可以锁定整个 repo,这只是锁定一个目录。
Is there a way to lock a specific branch in GIT?
有没有办法锁定 GIT 中的特定分支?
Or is there a way an Update Hook can identify from which branch the user is pushing and to which branch the code is pushed?
或者有没有一种方法可以让更新钩子识别用户从哪个分支推送以及代码被推送到哪个分支?
采纳答案by eckes
The branch being pushed to is the first parameter to the update hook. If you want to lock the branch myfeature
for pushing, this code (placed in hooks/update
) will do it:
被推送到的分支是更新钩子的第一个参数。如果要锁定分支myfeature
以进行推送,则此代码(放置在 中hooks/update
)将执行此操作:
#!/bin/sh
# lock the myfeature branch for pushing
refname=""
if [[ $refname == "refs/heads/myfeature" ]]
then
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
echo "You cannot push to myfeature! It's locked"
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
exit 1
fi
exit 0
回答by Cascabel
The update hook, from the docs:
来自文档的更新钩子:
The hook executes once for each ref to be updated, and takes three parameters:
- the name of the ref being updated,
- the old object name stored in the ref,
- and the new objectname to be stored in the ref.
该钩子为每个要更新的 ref 执行一次,并采用三个参数:
- 正在更新的 ref 的名称,
- 存储在 ref 中的旧对象名称,
- 以及要存储在 ref 中的新对象名。
So... yes, it knows exactly what branch is being pushed, and can simply check that parameter and exit failure if it doesn't want the branch pushed to.
所以......是的,它确切地知道正在推送哪个分支,并且可以简单地检查该参数并在不希望分支推送到的情况下退出失败。
And if you want to (intelligently) do this beforethe user has uploaded the objects, you can use the pre-receive hook:
如果您想(智能地)在用户上传对象之前执行此操作,您可以使用 pre-receive 钩子:
This hook executes once for the receive operation. It takes no arguments, but for each ref to be updated it receives on standard input a line of the format:
<old-value>
SP<new-value>
SP<ref-name>
LFwhere
<old-value>
is the old object name stored in the ref,<new-value>
is the new object name to be stored in the ref and<ref-name>
is the full name of the ref.
这个钩子为接收操作执行一次。它不接受任何参数,但是对于每个要更新的 ref,它在标准输入上接收一行格式如下:
<old-value>
SP<new-value>
SP<ref-name>
LF其中
<old-value>
是 ref 中存储的旧对象名称,<new-value>
是要存储在 ref 中的新对象名称, 是 ref<ref-name>
的全名。
(those are spaces and line-feed)
(那些是空格和换行符)
回答by amx
A tool like gitolite has this kind of feature I believe: http://github.com/sitaramc/gitolite
我相信像 gitolite 这样的工具具有这种功能:http: //github.com/sitaramc/gitolite
回答by JGC
You can use pre-committo do this. It has a built in no-commit-to-branch
hook that can be used to prevent commits to one or more branches.
您可以使用预提交来执行此操作。它有一个内置的no-commit-to-branch
钩子,可用于防止提交到一个或多个分支。
Setup
设置
The basic setup process is:
基本的设置过程是:
- Install using pip or brew (instructions at https://pre-commit.com/#install)
- Create a
.pre-commit-config.yaml
file in the root of your project (see below for a first draft) - Install the hooks into your git config by running
pre-commit install
.
- 使用 pip 或 brew 安装(https://pre-commit.com/#install 上的说明)
.pre-commit-config.yaml
在项目的根目录中创建一个文件(请参阅下面的初稿)- 通过运行将钩子安装到您的 git 配置中
pre-commit install
。
Basic config for protecting branches
保护分支的基本配置
Here is a basic config that includes just the no-commit-to-branch
hook:
这是一个仅包含no-commit-to-branch
钩子的基本配置:
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.2.5
hooks:
- id: no-commit-to-branch
args: ['--branch', 'master']
If you want to protect multiple branches you can use include multiple --branch
args in the argument list:
如果你想保护多个分支,你可以--branch
在参数列表中使用包含多个参数:
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.2.5
hooks:
- id: no-commit-to-branch
args: ['--branch', 'master', '--branch', 'staging']
Isn't this all overkill?
这不是矫枉过正吗?
Pre-commit has many other built-in hooks, and a large collection of community-built hooksthat will transform the way you clean-up and validate your commits. The reason I mention this is because, while this tool may be overkill for just preventing commits to a protected branch, it has many other features that make it a compelling and simple addition to any git project.
Pre-commit 有许多其他内置挂钩,以及大量社区构建的挂钩,它们将改变您清理和验证提交的方式。我提到这一点的原因是,虽然这个工具只是阻止提交到受保护的分支可能有点矫枉过正,但它还有许多其他功能,使其成为任何 git 项目的引人入胜且简单的补充。