Skip to content

Instantly share code, notes, and snippets.

@donatello
Last active October 12, 2017 13:37
Show Gist options
  • Save donatello/21e1a4761fc3611db57f90970f0a91ac to your computer and use it in GitHub Desktop.
Save donatello/21e1a4761fc3611db57f90970f0a91ac to your computer and use it in GitHub Desktop.
A git script to pull branches of github users to a local clone - for reviewing and testing Github PRs locally
#!/bin/bash
USER=$1
BRANCH=$2
function show_help {
cat <<HEREDOC
Summary:
Checkout a Github user's branch locally for review.
Usage:
git-ghrw USERNAME BRANCH
This script first checks out the master branch, updates it by pulling
in "upstream/master", then creates a new branch there named
"USERNAME-BRANCH". If a remote named "upstream" is not configured,
"origin" is tried instead.
Then it pulls in BRANCH from the Github user's fork, i.e. it executes:
git pull -q https://github.com/USERNAME/REPO_NAME BRANCH
The REPO_NAME is detected from the current directory.
HEREDOC
}
if [ -z $USER ]; then
echo "Please provide a GitHub username - changes from their fork will be pulled in"
show_help
exit 1
fi
if [ -z $BRANCH ]; then
echo "Please provide the branch of the user to pull in"
show_help
echo "Hello world"
exit 1
fi
set -e
GIT_DIR=$(git rev-parse --show-toplevel)
REPO_NAME=$(basename $GIT_DIR)
LOCAL_BRANCH=${USER}-${BRANCH}
if $(git remote | grep -q upstream); then
REMOTE=upstream
else
REMOTE=origin
fi
cd $GIT_DIR
echo "Pulling ${REMOTE}/master -> master and creating new branch $LOCAL_BRANCH."
git checkout -q master && git pull -q ${REMOTE} master && git checkout -q -B $LOCAL_BRANCH
echo "Pulling https://github.com/$USER/$REPO_NAME:$BRANCH"
git pull -q https://github.com/$USER/$REPO_NAME $BRANCH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment