Skip to content

Instantly share code, notes, and snippets.

@wookimiii
Last active September 15, 2021 22:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wookimiii/8188967 to your computer and use it in GitHub Desktop.
Save wookimiii/8188967 to your computer and use it in GitHub Desktop.
Check out a branch using grep
#!/bin/bash
BRANCHES=$(git branch -ra | grep $1)
NUM=$(echo "$BRANCHES" | wc -l)
# check for empty
if [ "X$BRANCHES" = "X" ]
then
echo "No branch matches $1"
fi
if [ $NUM -eq 1 ]
then
remote="remotes/origin/"
branch=$(echo $BRANCHES | awk '{ gsub(/remotes\/origin\//,""); print}')
git checkout $branch
fi
list=()
i=0
if [ $NUM -gt 1 ]
then
while read -r line; do
(( i++ ))
branch=$(echo $line | awk '{ gsub(/remotes\/origin\//,""); print}')
echo "$i: $branch"
list+=("$branch")
done <<< "$BRANCHES"
echo -n "Please choose one of the branches: "
read index
git checkout ${list[index-1]}
fi

Make this file executable and add it to your path

Usage

grco [some-text]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment