Git request before commit

Is there a way git will tell me: "Are you trying to commit in the production branch. Are you sure (y / N)?" message before each commit. As you can see from the message, I want it only on a certain branch (for example, a production branch) in order to avoid any false commits.

+5
source share
3 answers

Set your prompt accordingly and see the branch you are in - https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh

If you need better tools, for example, a git request, an invitation to the ruby ​​version, an invitation to a virtual virtual python operator, etc. try bash-it- https://github.com/revans/bash-it

If you want to find the exact solution that you are describing here, configure some script alias for git commit, configure a tag pre-commit, etc., which will see your current branch and offer you as needed.

+3
source

You can use the pre-commit hook to check if you are in a production branch, but hook scripts cannot collect information from the user. A possible alternative would be to state that the parameter --no-verify(or -n) for git commitshould be used to perform the commit.

.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
+5

, Git Hook. , .git/hooks, git , , .
"pre-commit" hook. git:

, . , , , - , , , . [...]

, - :

  • .git/hooks/pre-commit.sample .git/hooks/pre-commit ( hook)
  • Add the code to the script that gets the current branch name. Maybe something like: git status -sb | awk '{print $2}'(although there may be a better way).
  • Check if you are in the production branch, and if so, tell the user with y / n.
  • If the user selects no, simply exit with a non-zero code to abort the commit.
+1
source

All Articles