Skip to content

Instantly share code, notes, and snippets.

@zopieux
Last active December 2, 2022 15:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zopieux/5b8b327f320c78021ca3357619353421 to your computer and use it in GitHub Desktop.
Save zopieux/5b8b327f320c78021ca3357619353421 to your computer and use it in GitHub Desktop.
Clone GitHub forked repo and add upstream remote
#!/bin/bash
set -e
fatal() {
echo $@
exit 1
}
repo=$1
[[ "$repo" == https://github* ]] && repo=$(echo "$repo" | cut -d/ -f4-)
[[ "$repo" == *.git ]] && repo=$(echo "$repo" | cut -d. -f1)
data=$(curl -s "https://api.github.com/repos/$repo")
jqget() {
echo "$data" | jq -r "$1"
}
[[ $(jqget .fork) == true ]] || fatal not a fork
origin=$(jqget .ssh_url)
upstream=$(jqget .parent.clone_url)
name=$(jqget .name)
target=${2:-$name}
origin="git clone $origin $2"
upstream="git -C $target remote add upstream $upstream"
echo 'Will execute:'
echo " $origin"
echo " $upstream"
echo
read -n 1 -p 'Continue? ' yesno
echo
case $yesno in
y*|Y*)
$origin
$upstream
git -C "$target" remote -v
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment