如何允许git merge提交为主控,但阻止非合并提交? [英] How can I allow git merge commits to master but prevent non-merge commits?
问题描述
我有一个Git预提交钩子,可以防止我在不被覆盖的情况下提交给master,以鼓励在分支上进行开发.
I have a Git pre-commit hook that prevents me from committing to master unless overridden, in order to encourage developing on branch.
但是,我想自动允许将合并提交提交给master.有没有一种方法可以从我的预提交钩子脚本中识别出一个合并提交?该脚本如下所示:
However I would like to automatically allow merge commits to master. Is there a way to identify a merge commit from my pre-commit hook script? The script looks like this:
#!/bin/bash
BRANCH=`git branch --color=never| grep '^*'|cut -c3-`
if [ "${BRANCH}D" == "masterD" -a "${GIT_COMMIT_TO_MASTER}D" != "trueD" ]
then
echo "Commit directly to master is discouraged."
echo "If you want to do this, please set GIT_COMMIT_TO_MASTER=true and then commit."
exit 1
fi
已解决:对于需要剪切和粘贴的任何人,此挂钩脚本的工作版本为:
SOLVED: For anyone looking for a cut-and-paste, the working version of this hook script is:
#!/bin/bash
BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "${BRANCH}" == "master" -a "${GIT_COMMIT_TO_MASTER}" != "true" ]
then
if [ -e "${GIT_DIR}/MERGE_MODE" ]
then
echo "Merge to master is allowed."
exit 0
else
echo "Commit directly to master is discouraged."
echo "If you want to do this, please set GIT_COMMIT_TO_MASTER=true and then commit."
exit 1
fi
fi
推荐答案
我在其中添加了一些注释,但重要的是在 pre -commit钩子中,您所提交的是即将进行测试的人还不存在,因此您无法指望它的父母.
I put a few comments in, but the important one here is that in a pre-commit hook, the commit you're about to test does not yet exist, so you can't count its parents.
这就是你得到的:
-
如果您使用
git commit --amend
来修改合并提交,则像平常一样运行pre-commit挂钩,但是它无法真正检测到发生这种情况.新的提交将是合并,但您无法确定.
If you're using
git commit --amend
to amend a merge commit, the pre-commit hook is run as usual, but it can't really detect that this is happening. The new commit will be a merge, but you can't tell.
如果您使用常规的旧版git commit
创建非合并提交,则git目录中将不存在文件MERGE_HEAD
,您可以说这将不会创建合并提交.
If you're using regular old git commit
to create a non-merge commit, the file MERGE_HEAD
will not exist in the git directory, and you can tell that this is not going to create a merge commit.
如果使用git commit
完成冲突的合并,则文件MERGE_HEAD
将存在,并且您可以说这将创建合并提交.
If you're using git commit
to finish off a conflicted merge, the file MERGE_HEAD
will exist, and you can tell that this is going to create a merge commit.
如果您正在运行git merge
并且它成功运行,它将在不使用pre-commit钩子的情况下进行新的提交,因此您甚至在这里都不会被调用.
If you're running git merge
and it succeeds on its own, it makes a new commit without using the pre-commit hook, so you don't even get invoked here.
因此,如果您愿意允许合并中的git commit --amend
不起作用,则可以接近所需的值:只需测试$GIT_DIR/MERGE_HEAD
的存在,以查看这是否是正在完成的git commit
发生冲突的合并. (使用$GIT_DIR
是使该工作正常的技巧,即使命令在git树之外运行.Git也会设置$GIT_DIR
,以便挂机git命令可以正常工作.)
Hence, if you're willing to allow git commit --amend
on merges to misfire, you can get close to what you want: just test for the existence of $GIT_DIR/MERGE_HEAD
to see if this is a git commit
that is finishing off a conflicted merge. (The use of $GIT_DIR
is a trick to make this work even if the commands are run outside the git tree. Git sets $GIT_DIR
so that in-hook git commands will work right.)
这篇关于如何允许git merge提交为主控,但阻止非合并提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!