Skip to content

Instantly share code, notes, and snippets.

@mivanov
Created May 1, 2018 00:04
Show Gist options
  • Save mivanov/72b8fdd3ffd0ff5aa2ceb91cf8192091 to your computer and use it in GitHub Desktop.
Save mivanov/72b8fdd3ffd0ff5aa2ceb91cf8192091 to your computer and use it in GitHub Desktop.
Git pre-push hook to prevent accidentally pushing to shared repo
#!/usr/bin/python
#
# Pre-push hook which aborts the push if it does not find current user's username in the remote path.
# To set up, copy into a folder of git hooks and mark as executable. To apply globally (starting with Git v2.9)
# use:
# git config --global core.hooksPath=/path/to/hooks
import sys
import getpass
user = getpass.getuser()
hook, remote_name, remote_url = sys.argv
if user and user not in remote_url:
msg = 'WARNING: {} looks like a shared repo. Aborting push. To disable hooks pass --no-verify'.format(remote_name)
print(msg)
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment