How to stop "git push all" using git hook?

by giovanny.lueilwitz , in category: Third Party Scripts , 5 months ago

How to stop "git push all" using git hook?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by elise_daugherty , 5 months ago

@giovanny.lueilwitz 

To stop a git push all using a git hook, you can create a pre-push hook that checks the remote being pushed to and prevents the push if it contains the keyword "all".


Here's how you can create a pre-push hook to stop git push all:

  1. Navigate to the hooks directory in your git repository by running the following command:
1
cd /path/to/your/git/repository/.git/hooks


  1. Create a new file named pre-push in the hooks directory:
1
touch pre-push


  1. Open the pre-push file in a text editor and add the following code to prevent git push all:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#!/bin/bash

while read local_ref local_sha remote_ref remote_sha
do
  if [[ "$remote_ref" == *"all"* ]]; then
    echo "Error: Push to remote containing 'all' is not allowed."
    exit 1
  fi
done

exit 0


  1. Make the pre-push file executable by running the following command:
1
chmod +x pre-push


Now, whenever you try to push to a remote containing the keyword "all", the pre-push hook will prevent the push and display an error message.