4 ms·
There are "hooks" in git that can be used to modify its behavior, to e.g. "spit out a warning if you created a branch and then try to commit to master" This se
by rendall 6y ago
There are "hooks" in git that can be used to modify its behavior, to e.g. "spit out a warning if you created a branch and then try to commit to master"
This seems to be a good resource https://githooks.com/ https://githooks.com/
- matkoniecz 6y ago> "spit out a warning if you created a branch and then try to commit to master" How this one may be done? It sounds wonderful but I have no idea how to implement such thing (and if not included in git already, I hope that I can reuse code rather than reimplement it)
- deleted 6y ago[deleted]
- rendall 6y agoOh, hey now. Look at the very bottom of the resource I sent you, under "Snippets" https://githooks.com/ https://githooks.com/
- matkoniecz 6y ago"A pre-push hook that prevents direct code push to master branch" and I would want "spit out a warning if you created a branch and then try to commit to master" Sometimes I would want to commit to master, I want an overridable warning iff I just created branch.
- xmodem 6y agoHow about #!/bin/sh BRANCHES_POINTING_TO_HEAD=$(git branch --contains HEAD |wc -l) CURRENT_BRANCH=$(git branch --show-current) if [ "$BRANCHES_POINTING_TO_HEAD" -gt 1 ]; then if [ "$CURRENT_BRANCH" = "master" ]; then echo "You're committing to master even though other branches exist." echo "override with git commit --no-verify" exit 1 fi fi You'll need git 2.22 for the --show-current git option. But also I would suggest instead using git checkout -b <new_branch> to create and switch branches in one command
- dheera 6y agoA better UX would be $ git checkout <new_branch> <new_brach> doesn't exist. Would you like to create it (Y/n): The real problem with "git" is just way too many options. For automated tools there can be a "-n" non-interactive option that fails with an error code and doesn't ask questions.