Sometimes, I create local branches in git, and I’d like to get a warning message when I try to dcommit from them.
How can I prevent myself from accidentally dcommiting from a local branch?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
An alternative to pre-commit hooks, if you’re using Linux (or Git bash or Cygwin or similar), is to wrap
gitin a shell helper function. Add the below to your~/.bashrc(for bash, Git bash) or~/.zshrc(for zsh) file, or whatever the equivalent is for your shell:(I’ve tested this with bash and zsh on Red Hat, and bash on Cygwin)
Whenever you call
git, you’ll now be calling this function rather than the normal binary. The function will run git normally, unless you’re callinggit svn dcommitwhile attached to a branch that’s not master. In that case, it’ll prompt you to confirm before doing the commit. You can override the function by specifying the path togitexplicitly (that’s what the$real_gitis doing).Remember that after updating
~/.bashrcor equivalent, you’ll need to reload it, either by starting a new shell session (logging out and logging in again) or by runningsource ~/.bashrc.Edit: As an enhancement, you can remove the first line, starting
real_git=, and replace the other instances of$real_gitwithcommand git, which achieves the same thing but in the preferred way. I’ve not updated the script itself as I’ve not been able to test the change on zsh.