bash 如何使用 git hook pre-commit 停止向 master 提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7923505/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:00:31  来源:igfitidea点击:

How to use git hook pre-commit to stop commits to master

gitbashgithooks

提问by Adrian Cornish

I want to stop myself accidently commiting something to the master branch unless I am sure. So I tried this script to determine which branch I am on but there is a problem. When I create a new branch git name-rev returns master even though I am on the other branch

除非我确定,否则我想阻止自己意外地向 master 分支提交一些东西。所以我尝试了这个脚本来确定我所在的分支,但有一个问题。当我创建一个新分支时,即使我在另一个分支上,git name-rev 也会返回 master

$ git branch
  ignore
  master
* set_support
$ git name-rev --name-only HEAD
master

This is my script.

这是我的脚本。

#!/bin/sh
# Check to see if we are on master branch. Stop accidental commits
if [ "`git name-rev --name-only HEAD`" == "master" ]
then
   if [ -f i_want_to_commit_to_master ]
   then
      rm i_want_to_commit_to_master
      exit 0
   else
      echo "Cannot commit to master branch Adrian"
      echo "Remember to create file 'touch i_want_to_commit_to_master' to commit to master"
   fi
   exit 1
fi
exit 0

For Mark: I rebuilt git against latest stable tag and same results. It only works after a commit is made to the new branch.

对于 Mark:我根据最新的稳定标签重建了 git,结果相同。它仅在对新分支进行提交后才有效。

$ mkdir gittest
$ cd gittest
$ git init
Initialized empty Git repository in /home/adrian/gittest/.git/
$ touch file1
$ git add file1
$ git commit
[master (root-commit) 7c56424] New file
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1
$ git branch
* master
$ git checkout -b new_branch
Switched to a new branch 'new_branch'
$ git name-rev --name-only HEAD
master
$ git --version
git version 1.7.7.1
$ git branch
  master
* new_branch
$ touch file2
$ git add file2
$ git commit
[new_branch 1e038fb] new file
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file2
$ git name-rev --name-only HEAD
new_branch

回答by Adam Dymitruk

This command is used to find a friendly name of a commit. What is happening is that HEAD is resolving to the sha1 of the commit first and then a name is determined. I'm guessing it is arbitrarily picking master for the name as it comes up first in what git log --decoratewould come across.

此命令用于查找提交的友好名称。发生的事情是 HEAD 首先解析提交的 sha1,然后确定名称。我猜它是随意选择主人作为名字,因为它首先出现在git log --decorate会遇到的东西中。

I would just parse the output of git branchin your test:

我只想解析git branch您测试中的输出:

"`git branch | grep \* | cut -f2 -d' '` == "master"

or a more direct way would be:

或者更直接的方法是:

$(git symbolic-ref HEAD 2>/dev/null) == "refs/heads/master"

回答by Diego

As an alternative you could use git rev-parseas suggested in this answer. So the if expression would be:

作为替代方案,您可以git rev-parse按照本答案中的建议使用。所以 if 表达式将是:

"$(git rev-parse --abbrev-ref HEAD)" == "master"