Skip to content

Instantly share code, notes, and snippets.

@riemannulus
Last active January 10, 2021 05:29
Show Gist options
  • Save riemannulus/f7287a90720fcef76d188056d53bb5d4 to your computer and use it in GitHub Desktop.
Save riemannulus/f7287a90720fcef76d188056d53bb5d4 to your computer and use it in GitHub Desktop.
Add upstream
function add_upstream() {
url=$(git config --get remote.origin.url)
repo=${url##*/}
if [[ "$repo" =~ ".git" ]]; then
repo=$(echo "$repo" | awk -F. '{print $1}')
fi
user=$(echo "$url" | awk -F/ '{print $4}')
if [ -z "$user" ]; then
user=$(echo "$url" | awk -F: '{print $2}' | awk -F/ '{print $1}')
fi
remote=$(curl -s "https://api.github.com/repos/$user/$repo" | jq -r '.parent.clone_url')
if [ "$remote" != "null" ]; then
git remote add upstream "$remote"
else
echo "no upstream found"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment