Skip to content

Instantly share code, notes, and snippets.

@andrewsouthard
Last active April 11, 2024 17:39
Show Gist options
  • Select an option

  • Save andrewsouthard/a3f3cc7604f4afb0f65a66344e998813 to your computer and use it in GitHub Desktop.

Select an option

Save andrewsouthard/a3f3cc7604f4afb0f65a66344e998813 to your computer and use it in GitHub Desktop.

Revisions

  1. andrewsouthard revised this gist Apr 11, 2024. 1 changed file with 3 additions and 6 deletions.
    9 changes: 3 additions & 6 deletions Prevent accidental push to main
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,6 @@
    If you forget to checkout a branch, you can use a pre-push hook to stop you before pushing to main
    cat ~/.git-templates/hooks/pre-push
    ```
    #!/bin/bash

    # If you forget to checkout a branch, you can use a pre-push hook to stop you before pushing to main
    # Put this at ~/.git-templates/hooks/pre-push
    PROTECTED_BRANCH=('main' 'master')
    CURRENT_BRANCH=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

    @@ -16,5 +14,4 @@ if [[ " ${PROTECTED_BRANCH[@]} " == *" ${CURRENT_BRANCH} "* ]]; then
    exit 1 # push will not execute
    else
    exit 0 # push will execute
    fi
    ```
    fi
  2. andrewsouthard revised this gist Apr 11, 2024. 2 changed files with 20 additions and 1 deletion.
    20 changes: 20 additions & 0 deletions Prevent accidental push to main
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    If you forget to checkout a branch, you can use a pre-push hook to stop you before pushing to main
    cat ~/.git-templates/hooks/pre-push
    ```
    #!/bin/bash

    PROTECTED_BRANCH=('main' 'master')
    CURRENT_BRANCH=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

    if [[ " ${PROTECTED_BRANCH[@]} " == *" ${CURRENT_BRANCH} "* ]]; then
    read -p "You're about to push to ${CURRENT_BRANCH}, is that what you intended? [y|n] " -n 1 -r < /dev/tty
    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
    ```
    1 change: 0 additions & 1 deletion test.txt
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    this is a test
  3. andrewsouthard created this gist May 22, 2022.
    1 change: 1 addition & 0 deletions test.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    this is a test