Skip to content

Instantly share code, notes, and snippets.

@cb-bobin
Forked from mosra/README.md
Created February 16, 2021 14:53
Show Gist options
  • Save cb-bobin/28518754b544f9a9f1959b4243a49286 to your computer and use it in GitHub Desktop.
Save cb-bobin/28518754b544f9a9f1959b4243a49286 to your computer and use it in GitHub Desktop.

Revisions

  1. @mosra mosra revised this gist Feb 16, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pre-push
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ protected_branch='master'
    # Argument parsing taken from .git/hooks/pre-push.sample
    if read local_ref local_sha remote_ref remote_sha; then
    if [[ "$remote_ref" == *"$protected_branch"* ]]; then
    echo -en "\033[1;33mYou're about to push master, is that what you intended? [y|n] \033[0m"
    echo -en "\033[1;33mYou're about to push to master, is that what you intended? [y|n] \033[0m"
    echo -en "\033[1m"
    read -n 1 -r < /dev/tty
    echo -en "\033[0m"
  2. @mosra mosra revised this gist Feb 16, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ Further info: https://dev.ghost.org/prevent-master-push/, https://coderwall.com/
    4. Make it executable

    ```sh
    chmod a+x ~/.git-templates/hooks/pre-push
    chmod +x ~/.git-templates/hooks/pre-push
    ```

    5. In currently existing project, do a reinit. This will not overwrite existing commits, or existing hooks -- so if you already have a `pre-push` hook, you may need to merge the two together.
  3. @mosra mosra revised this gist Feb 16, 2020. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -11,22 +11,29 @@ Further info: https://dev.ghost.org/prevent-master-push/, https://coderwall.com/
    ## Installation

    1. Enable git templates

    ```sh
    git config --global init.templatedir '~/.git-templates'
    ```

    2. Create a directory to hold the global hooks:

    ```sh
    mkdir -p ~/.git-templates/hooks
    ```

    3. Copy the `pre-push` file from below into `~/.git-templates/hooks/pre-push`

    4. Make it executable

    ```sh
    chmod a+x ~/.git-templates/hooks/pre-push
    ```
    5. In currently existing project, do reinit

    5. In currently existing project, do a reinit. This will not overwrite existing commits, or existing hooks -- so if you already have a `pre-push` hook, you may need to merge the two together.

    ```sh
    git init
    ````
    This will not overwrite existing commits, or existing hooks -- so if you already have a `pre-push` hook, you may need to merge the two together.
    Done!
  4. @mosra mosra revised this gist Feb 16, 2020. 2 changed files with 38 additions and 51 deletions.
    55 changes: 22 additions & 33 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,43 +1,32 @@
    # Git pre-push hook

    It will check if current branch is master, then ask a confirmation, in case of `master` branch

    Articles with initial info: https://dev.ghost.org/prevent-master-push/, https://coderwall.com/p/jp7d5q/create-a-global-git-commit-hook

    ## Demo

    http://imgur.com/a/EXGAW

    ## To install

    1. Enable git templates
    ```
    git config --global init.templatedir '~/.git-templates'
    Checks if the remote branch is `master`, then asks a confirmation. Based on https://gist.github.com/ColCh/9d48693276aac50cac37a9fce23f9bda, but modified to check the remote name instead of local, making it work also for the really dangerous accidents like below:

    ```sh
    git push -f origin e09b7418a310114f6aaf495f969c3859095a99af:master
    ```

    2. Create a directory to hold the global hooks:

    ```
    mkdir -p ~/.git-templates/hooks
    ```
    Further info: https://dev.ghost.org/prevent-master-push/, https://coderwall.com/p/jp7d5q/create-a-global-git-commit-hook, https://git-scm.com/docs/githooks#_pre_push, https://stackoverflow.com/questions/22585091/git-hooks-pre-push-script-does-not-receive-input-via-stdin

    3. Write your hook in ~/.git-templates/hooks

    See file `pre-push` in this gist.

    Copy it to `~/.git-templates/hooks/pre-push`
    ## Installation

    1. Enable git templates
    ```sh
    git config --global init.templatedir '~/.git-templates'
    ```
    2. Create a directory to hold the global hooks:
    ```sh
    mkdir -p ~/.git-templates/hooks
    ```
    3. Copy the `pre-push` file from below into `~/.git-templates/hooks/pre-push`
    4. Make it executable

    ```
    chmod a+x ~/.git-templates/hooks/pre-push
    ```

    5. In currently existing project, do reinit

    `git init`

    This will not overwrite existing commits, or existing hooks.
    ```sh
    chmod a+x ~/.git-templates/hooks/pre-push
    ```
    5. In currently existing project, do reinit
    ```sh
    git init
    ````
    This will not overwrite existing commits, or existing hooks -- so if you already have a `pre-push` hook, you may need to merge the two together.
    Done!
    34 changes: 16 additions & 18 deletions pre-push
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,19 @@
    #!/bin/bash
    protected_branch='master'
    current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

    if [ $protected_branch = $current_branch ]
    then

    echo -en "\033[31mYou're about to push master, is that what you intended? [y|n] \033[0m"
    echo -en "\033[1m"
    read -n 1 -r < /dev/tty
    echo -en "\033[0m"

    echo
    if echo $REPLY | grep -E '^[Yy]$' > /dev/null
    then
    exit 0 # push will execute
    protected_branch='master'

    # Argument parsing taken from .git/hooks/pre-push.sample
    if read local_ref local_sha remote_ref remote_sha; then
    if [[ "$remote_ref" == *"$protected_branch"* ]]; then
    echo -en "\033[1;33mYou're about to push master, is that what you intended? [y|n] \033[0m"
    echo -en "\033[1m"
    read -n 1 -r < /dev/tty
    echo -en "\033[0m"

    echo
    if echo $REPLY | grep -E '^[Yy]$' > /dev/null; then
    exit 0 # push will execute
    fi
    exit 1 # push will not execute
    fi
    exit 1 # push will not execute
    else
    exit 0 # push will execute
    fi
    fi
  5. @ColCh ColCh revised this gist Mar 29, 2017. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,14 @@
    # Demo
    # Git pre-push hook

    http://imgur.com/a/EXGAW
    It will check if current branch is master, then ask a confirmation, in case of `master` branch

    Articles with initial info: https://dev.ghost.org/prevent-master-push/, https://coderwall.com/p/jp7d5q/create-a-global-git-commit-hook

    # To install
    ## Demo

    http://imgur.com/a/EXGAW

    Enable git templates for future `git init` : https://coderwall.com/p/jp7d5q/create-a-global-git-commit-hook
    ## To install

    1. Enable git templates
    ```
  6. @ColCh ColCh created this gist Mar 29, 2017.
    39 changes: 39 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    # Demo

    http://imgur.com/a/EXGAW

    # To install

    Enable git templates for future `git init` : https://coderwall.com/p/jp7d5q/create-a-global-git-commit-hook

    1. Enable git templates
    ```
    git config --global init.templatedir '~/.git-templates'
    ```

    2. Create a directory to hold the global hooks:

    ```
    mkdir -p ~/.git-templates/hooks
    ```

    3. Write your hook in ~/.git-templates/hooks

    See file `pre-push` in this gist.

    Copy it to `~/.git-templates/hooks/pre-push`

    4. Make it executable

    ```
    chmod a+x ~/.git-templates/hooks/pre-push
    ```

    5. In currently existing project, do reinit

    `git init`

    This will not overwrite existing commits, or existing hooks.

    Done!
    21 changes: 21 additions & 0 deletions pre-push
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    #!/bin/bash
    protected_branch='master'
    current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

    if [ $protected_branch = $current_branch ]
    then

    echo -en "\033[31mYou're about to push master, is that what you intended? [y|n] \033[0m"
    echo -en "\033[1m"
    read -n 1 -r < /dev/tty
    echo -en "\033[0m"

    echo
    if echo $REPLY | grep -E '^[Yy]$' > /dev/null
    then
    exit 0 # push will execute
    fi
    exit 1 # push will not execute
    else
    exit 0 # push will execute
    fi