Skip to content

Instantly share code, notes, and snippets.

@AWhetter
Created October 2, 2017 17:38
Show Gist options
  • Save AWhetter/19aa411c35cf08a1e670c66aba914ce3 to your computer and use it in GitHub Desktop.
Save AWhetter/19aa411c35cf08a1e670c66aba914ce3 to your computer and use it in GitHub Desktop.
fetchout subcommand for git that fetches and checks out the new branch
#!/bin/bash
get_branches() {
git branch -r | tail -n +2 | sed "s|^[^/]*/||" | sort
}
branches_were=$(get_branches)
git fetch
branches_are=$(get_branches)
mapfile -t choices < <(diff --old-line-format="" --unchanged-line-format="" <(echo "$branches_were") <(echo "$branches_are"))
if [ ${#choices[@]} -eq 1 ]
then
git checkout "${choices[0]}"
exit 0
elif (( ${#choices[@]} ))
then
echo
select branch in "${choices[@]}"
do
git checkout "$branch"
exit 0
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment