本文介绍了git钩防止合并特定分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个分支,一个生产分支和一个主分支.我想防止合并到生产的任何新分支,这些分支从 master 和master 自身签出.
I have two branches, a production branch, and a master branch. I want to prevent merging into production any new branch that checked out from master and master itself.
有一个git pre钩子可以帮上忙吗?
There is a git pre hook that can help with that?
推荐答案
- 在.git/hooks/文件夹中创建prepare-commit-msg
- 设置chmod权限:
chmod +x .git/hooks/prepare-commit-msg
- 禁用快进合并:
git config branch.master.mergeoptions "--no-ff"
- create the prepare-commit-msg into your .git/hooks/ folder
- make chmod permissions:
chmod +x .git/hooks/prepare-commit-msg
- disable fast-forward merges:
git config branch.master.mergeoptions "--no-ff"
.git/hooks/prepare-commit-msg
#!/usr/bin/env ruby
# Only for exceptions. In your case is empty array
WHITELIST_BRANCH = ["hotfix"]
def merge?
ARGV[1] == "merge"
end
def into_master?
current_branch = `git branch | grep '*' | sed 's/* //'`.chop
current_branch == "master"
end
def merge_msg
@msg ||= `cat .git/MERGE_MSG`
end
def from_branch
@from_branch = merge_msg.match(/Merge branch '(.*?)'/)[1]
end
def whitelist_branch?
WHITELIST_BRANCH.include?(from_branch)
end
if merge? && into_master? && !whitelist_branch?
out = `git reset --merge`
puts
puts " Fail #{from_branch} into the *master* branch "
puts
puts " run the following command now to discard your working tree changes:"
puts
puts " git reset --merge"
puts
exit 1
end
在此代码中,仅WHITELIST_BRANCH可用于合并
In this code only WHITELIST_BRANCH is available to merge
这篇关于git钩防止合并特定分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!