Is there a way git prompts me a "You are attempting to make a commit in the production branch. Are you sure (y/N)?" message before every commit. As it is obvious from the message, I want it only on a specific branch (say production branch) to avoid any spurious commits.
You could use a pre-commit hook to check if you're on the production branch,
but hook scripts can't collect information from the user. A possible
alternative would be to state that the --no-verify
(or -n
) option to git
commit
should be used to really do the commit.
This could be done with something like the following in .git/hooks/pre-commit
:
#/bin/sh
case "$(git rev-parse --symbolic-full-name HEAD)" in
refs/heads/production)
echo 'Use `git commit --no-verify` to commit to production branch'
exit 1
;;
esac
Set your prompt appropriately and see the branch that you are in always - https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh
If you want better tools like better git prompt, ruby version prompt, python virtualenv prompt, etc. try bash-it
- https://github.com/revans/bash-it
If you want the exact solution that you describe here, set up some alias script for git commit, setup a pre-commit
hook etc which will see your current branch and prompt you as needed.
Sounds like you could use a Git Hook.
These are scripts located in .git/hooks that git can run when certain events occur, such as during various stages of the commit process.
One such hook is the 'pre-commit' hook. From the git book:
The pre-commit hook is run first, before you even type in a commit message. It’s used to inspect the snapshot that’s about to be committed, to see if you’ve forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code. Exiting non-zero from this hook aborts the commit [...]
So, you could do something like this:
git status -sb | awk '{print $2}'
(although there might be a better way).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With