Git hook to prevent accidental work on master¶
https://www.git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
https://stackoverflow.com/questions/3417896/how-do-i-prompt-the-user-from-within-a-commit-msg-hook
Hooks in git can be found in .git/hooks. There are various types of hooks, but for this specific case the ones we need are pre-commit and pre-push. These hooks do not exist by default, only .sample versions of them which do not do anything.
For a hook to be usable, it must be executable. You can do so using chmod +x {filename}.
#!/bin/sh branch=$(git branch --show-current) # Allows us to read user input below, assigns stdin to keyboard exec < /dev/tty if [[ $branch == "master" ]]; then echo -en "\e[33mYou are working on the master branch.\e[0m Continue? [y/N] " read response if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then exit 0 else exit 1 fi fi
Create a pre-commit or pre-push (or both, if you wish) file in your repository’s .git/hooks directory, paste the content of the code block above and make it executable.