比如在開發中不希望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的時候就會被阻止了.