git 阻止在某個分支上面提交commit

比如在開發中不希望master分支被commit做提交,那麼我們可以這樣做

找到 .git/hook/文件夾 然後在裏面複製一個 pre-commit出來

cd .git/hooks/
cp pre-commit.sample pre-commit

然後編輯它的第二行類似於這樣

#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".

branch="$(git rev-parse --abbrev-ref HEAD)"
if [ "$branch" = "master" ]; then
  echo "You can't commit to master!"
  exit 1
fi

在git add後 git commit的時候就會被阻止了.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章