前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >git禁止在master分支push和commit

git禁止在master分支push和commit

作者头像
职场亮哥
发布2020-10-10 15:48:55
6.3K0
发布2020-10-10 15:48:55
举报
文章被收录于专栏:职场亮哥

作为管理者,在远端将master分支设为保护分支,可以从根源上杜绝直接推送到master的问题。dev分支同理。

作为开发者,在本地的git hook中加配置可以做到在commit和push操作时做对应的检查

禁止在master分支上Commit

代码语言:javascript
复制
#!/bin/sh

protected_branch='master'
current_branch=$(git rev-parse --symbolic --abbrev-ref HEAD)

if [ "$protected_branch" == "$current_branch" ]; then
  echo ".git/hooks: Do not commit to $current_branch branch"
  exit 1
fi

在master分支上Commit时提示

代码语言:javascript
复制
#!/bin/sh

protected_branch='master'
current_branch=$(git rev-parse --symbolic --abbrev-ref HEAD)

if [ "$protected_branch" == "$current_branch" ]; then
  read -p "You're about to commit to master, is that what you intended? [y|n] " -n 1 -r </dev/tty
  echo
  if echo "$REPLY" | grep -E '^[Yy]$' >/dev/null; then
    exit 0 # commit will execute
  fi
  exit 1 # commit will not execute
fi

禁止推送到master分支

代码语言:javascript
复制
#!/bin/sh

protected_branch='master'
remote_branch_prefix="refs/heads/"
protected_remote_branch=$remote_branch_prefix$protected_branch

while read local_ref local_sha remote_ref remote_sha
do
	if [ "$protected_remote_branch" == "$remote_ref" ]; then
		echo ".git/hooks: Do not commit to $protected_branch branch"
	  exit 1
	fi
done

exit 0

推送到master分支时提示

代码语言:javascript
复制
#!/bin/sh

protected_branch='master'
remote_branch_prefix="refs/heads/"
protected_remote_branch=$remote_branch_prefix$protected_branch

while read local_ref local_sha remote_ref remote_sha
do
	if [ "$protected_remote_branch" == "$remote_ref" ]; then
		read -p "You're about to push master, is that what you intended? [y|n] " -n 1 -r < /dev/tty
    echo
    if echo $REPLY | grep -E '^[Yy]$' > /dev/null
    then
        exit 0 # push will execute
    fi
    exit 1 # push will not execute
	fi
done

exit 0

为什么需要循环读取?因为git一次可以push多个分支

推送时如果commit消息包含WIP则禁止推送

代码语言:javascript
复制
#!/bin/sh

z40=0000000000000000000000000000000000000000

while read local_ref local_sha remote_ref remote_sha; do
  if [ "$local_sha" = $z40 ]; then
    # Handle delete
    :
  else
    if [ "$remote_sha" = $z40 ]; then
      # New branch, examine all commits
      range="$local_sha"
    else
      # Update to existing branch, examine new commits
      range="$remote_sha..$local_sha"
    fi

    # Check for WIP commit
    commit=$(git rev-list -n 1 --grep '^feat: WIP' "$range")
    if [ -n "$commit" ]; then
      echo >&2 "Found WIP commit in $local_ref, not pushing"
      exit 1
    fi
  fi
done

exit 0

这时候,你可能会发现,你每一次clone项目之后都需要手动把commit和push的hook文件丢在.git/hooks目录下,是不是觉得不方便?别着急,有办法,我们可以让所有项目的hook操作统一到一个自定义目录中。

代码语言:javascript
复制
mkdir ~/.git-hooks	# 创建一个存放hook的自定义目录
git config --global core.hooksPath ~/.git-hooks	# 更改git配置指定hook目录到自定义,先别着急执行,往后看

这样就可以实现统一管理所有项目的hooks操作了

然后,你会觉得全局统一管理也太霸道了吧,比如说,公司的项目可以统一一套hooks操作,但是我不想把这一套hooks应用于个人github的项目啊。也就是说你需要在不同的目录下面执行不同的hooks操作,那么该怎么办呢?还是有办法:git配置是可以根据不同目录使用不同配置的

比如我只想统一管理~/yy目录下的所有项目,那就修改~/.gitconfig文件加入以下内容

代码语言:javascript
复制
[includeIf "gitdir:~/yy/"]
    path = .gitconfig-yy

然后增加一个~/.gitconfig-yy文件,在这个文件中加入yy目录下面的独有配置

代码语言:javascript
复制
[core]
    hookspath = ~/.git-hooks

参考:

  1. https://stackoverflow.com/questions/42455506/in-pre-push-hook-get-git-push-command-full-content
  2. https://www.geek-share.com/detail/2776108340.html
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 禁止在master分支上Commit
  • 在master分支上Commit时提示
  • 禁止推送到master分支
  • 推送到master分支时提示
  • 推送时如果commit消息包含WIP则禁止推送
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档