Confirmation of git push command
How I can configure some confirmation on git push command in concrete branch? I have production branch and sometimes I forgot that I in pro开发者_高级运维duction branch and push not those changes.
One potential solution would be to remove the config associated with 'production' branch.
git config --unset branch.<branch>.remote
git config --unset branch.<branch>.merge
That way, you have to mention to what remote you want to pull from (or push to, for that matter).
If you define an alias for the pull which:
- pull
- and then remove the remote and merge config for that branch,
you are sure you will not be able, for that branch, to do a simple "git pull" without entering additional parameters.
All you need is the pre-push hook for the commits into protected branches like "master".
Git offers a number of hooks for various actions (as written in the documention). The hooks are shell scripts with special inputs called at specific times by git so you can adapt them as you want, but to ask a confirmation before pushing to master you can do this:
protected_ref="refs/heads/master"
while read local_ref local_sha remote_ref remote_sha
do
if [ "$remote_ref" = "$protected_ref" ]; then
echo "Pushing to master, are you sure? (y/n)"
read confirmation < /dev/tty
if [ "$confirmation" != "y" ]; then
echo >&2 "$confirmation is not 'y', cancelling push"
exit 1
fi
fi
done
exit 0
Note that when asking for user input, you have to give it a terminal to read from, as explained in the question How to ask for user input in a Git hook?.
PS: GIT Version 1.8.2 or above needed for hooks to be available.
You can git fetch
, then git status
will tell you if new commits have been pushed.
精彩评论