Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git prompting me before commit

Tags:

git

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.

like image 914
Rajat Avatar asked Mar 21 '13 06:03

Rajat


3 Answers

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
like image 117
qqx Avatar answered Nov 13 '22 23:11

qqx


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.

like image 3
manojlds Avatar answered Nov 13 '22 22:11

manojlds


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:

  • Rename .git/hooks/pre-commit.sample to .git/hooks/pre-commit (to enable the hook)
  • Add some code in the to the script that gets the current branch name. Maybe something like: git status -sb | awk '{print $2}' (although there might be a better way).
  • Check if you're on the production branch, and if so prompt the user with y/n.
  • If the user selects 'no', just exit with a non-zero code to abort the commit.
like image 1
avivr Avatar answered Nov 13 '22 22:11

avivr