如何防止开发人员推动特定的分支? [英] How to prevent developers to push on a particular branch?

查看:138
本文介绍了如何防止开发人员推动特定的分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们最近将功能名称从 foo 更改为。这是一个很好的机会,在最新的 master 状态下创建一个新的分支,从中进行工作。



之前的分支需要保留它的历史记录,我现在还不能删除。

因此,我会喜欢找到一种机制,防止混淆的开发人员推动对错误分支的更改。我的窍门是:

$ $ p $ $ git checkout feature / foo
$ git commit --allow-empty -mWARNING :不要在这个分支上提交
$ git push

不幸的是我更喜欢解决方案像

  $ git branch --lock feature / foo -m使用功能/栏代替
$ git push

我可以使用哪些其他选项?

注意:我也可以重命名当前分支 git branch -m feature / foo feature / bar ,但这是关闭的主题。

解决方案

这听起来像是你想要使用git钩子。 预推钩子可以很容易地指定这样一个 $ b

.git / hooks 文件夹中,创建一个名为的脚本,推。内容应该是一个Bash脚本,如果你想阻止推送,返回1。我的第一个想法是这样的:

 #!/ bin / bash 
locked_branch ='feature / foo '
push_branch = $(git rev-parse --abbrev-ref HEAD)

if [$ locked_branch=$ push_branch]
然后
回显不要推这个分支!使用功能/栏代替
exit 1
fi
exit 0

编辑:

当然,上面的约束条件是没有合适的服务器可以运行钩子。然而,在服务器存在的情况下,它应该是服务器端的 update 脚本执行此操作,其中第一个参数是要更新的分支。因此,如果[$ locked_branch=$ 1]
,它可以与像

 根据发布的第一个脚本,c $ c> 

而不是 push_branch 变量。 / p>

We've recently changed feature name from foo to bar. This is a good opportunity to create a new branch at the latest master state from which the work will go on.

The previous branch needs to be kept for its history and I cannot be deleted yet.

Therefore I would like to find a mechanism that prevent confused developers to push changes on the wrong branch. My trick is:

$ git checkout feature/foo
$ git commit --allow-empty -m "WARNING: DO NOT COMMIT on this branch"
$ git push

Unfortunately I would prefer a solution like

$ git branch --lock feature/foo -m "Use feature/bar instead"
$ git push  

What other option could I use?

Note: I can also rename the current branch git branch -m feature/foo feature/bar, but this is off topic.

解决方案

This sounds like something you would want to use git hooks for. The pre-push hook can quite easily specify such a limitation.

In the .git/hooks folder, create a script called pre-push. The contents should be a Bash script that returns 1 in case you want to prevent the push. My first idea of it is along these lines:

#!/bin/bash
locked_branch='feature/foo'
push_branch=$(git rev-parse --abbrev-ref HEAD)

if [ "$locked_branch" = "$push_branch" ]
then
    echo "Do not push this branch! Use feature/bar instead"
    exit 1
fi
exit 0

EDIT:

The above is of course given the constraint that there's no proper server that would run hooks. In the presence of a server however, it should be the update script on the server-side enforcing this, the first parameter of which is the branch being updated. So it would work with a check like

if [ "$locked_branch" = "$1" ]

instead of having a push_branch variable, based on the first script posted.

这篇关于如何防止开发人员推动特定的分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆