Skip to content

Instantly share code, notes, and snippets.

@jhnns
Last active June 27, 2025 08:11
Show Gist options
  • Select an option

  • Save jhnns/d654d9d6da6d3b749986 to your computer and use it in GitHub Desktop.

Select an option

Save jhnns/d654d9d6da6d3b749986 to your computer and use it in GitHub Desktop.

Revisions

  1. jhnns revised this gist Sep 24, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git-pr
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ if [[ $id = "clean" ]]; then
    git branch -D ${ref#refs/heads/}
    done
    exit 0
    elif [[ $id =~ ^(https?://[^/]+/(.+))/pull/([0-9]+)$ ]]; then
    elif [[ $id =~ ^(https?://[^/]+/(.+))/pull/([0-9]+).*$ ]]; then
    remote=${BASH_REMATCH[1]}.git
    id=${BASH_REMATCH[3]}
    branch=pr/${BASH_REMATCH[2]}/$id
  2. jhnns revised this gist Sep 24, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions git-pr
    Original file line number Diff line number Diff line change
    @@ -32,6 +32,6 @@ else
    fi


    echo "git fetch $remote pull/$id/head:$branch"
    git fetch $remote pull/$id/head:$branch
    echo "git fetch -fu $remote pull/$id/head:$branch"
    git fetch -fu $remote pull/$id/head:$branch
    git checkout $branch
  3. jhnns revised this gist Sep 24, 2015. 1 changed file with 11 additions and 3 deletions.
    14 changes: 11 additions & 3 deletions git-pr
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,13 @@
    #!/bin/bash

    if [ -z "$1" ]; then
    echo "Usage: git pr [<remote>] <id-or-url>"
    echo "Usage: git pr [clean] [<remote>] <id-or-url>"
    echo ""
    echo "Examples:"
    echo "git pr 42 --> git fetch origin pull/42/head:pr/origin/42"
    echo "git pr upstream 42 --> git fetch upstream pull/42/head:pr/upstream/42"
    echo "git pr https://github.com/peerigon/phridge/pull/1 --> git fetch https://github.com/peerigon/phridge.git pull/1/head:pr/peerigon/phridge/1"
    echo "git pr clean --> Deletes all branches that match pr/*/* and pr/*/*/*"
    exit 1
    elif [ -z "$2" ]; then
    id=$1
    @@ -16,7 +17,13 @@ else
    remote=$1
    fi

    if [[ $id =~ ^(https?://[^/]+/(.+))/pull/([0-9]+)$ ]]; then
    if [[ $id = "clean" ]]; then
    git for-each-ref refs/heads/pr/*/* refs/heads/pr/*/*/* --format='%(refname)' | while read ref; do
    echo "git branch -D ${ref#refs/heads/}"
    git branch -D ${ref#refs/heads/}
    done
    exit 0
    elif [[ $id =~ ^(https?://[^/]+/(.+))/pull/([0-9]+)$ ]]; then
    remote=${BASH_REMATCH[1]}.git
    id=${BASH_REMATCH[3]}
    branch=pr/${BASH_REMATCH[2]}/$id
    @@ -26,4 +33,5 @@ fi


    echo "git fetch $remote pull/$id/head:$branch"
    git fetch $remote pull/$id/head:$branch
    git fetch $remote pull/$id/head:$branch
    git checkout $branch
  4. jhnns revised this gist Sep 24, 2015. No changes.
  5. jhnns created this gist Sep 24, 2015.
    29 changes: 29 additions & 0 deletions git-pr
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #!/bin/bash

    if [ -z "$1" ]; then
    echo "Usage: git pr [<remote>] <id-or-url>"
    echo ""
    echo "Examples:"
    echo "git pr 42 --> git fetch origin pull/42/head:pr/origin/42"
    echo "git pr upstream 42 --> git fetch upstream pull/42/head:pr/upstream/42"
    echo "git pr https://github.com/peerigon/phridge/pull/1 --> git fetch https://github.com/peerigon/phridge.git pull/1/head:pr/peerigon/phridge/1"
    exit 1
    elif [ -z "$2" ]; then
    id=$1
    remote=origin
    else
    id=$2
    remote=$1
    fi

    if [[ $id =~ ^(https?://[^/]+/(.+))/pull/([0-9]+)$ ]]; then
    remote=${BASH_REMATCH[1]}.git
    id=${BASH_REMATCH[3]}
    branch=pr/${BASH_REMATCH[2]}/$id
    else
    branch=pr/$remote/$id
    fi


    echo "git fetch $remote pull/$id/head:$branch"
    git fetch $remote pull/$id/head:$branch