Navigate local git branches with ease

yoniweisbrod

Yoni Weisbrod

Posted on October 27, 2020

Navigate local git branches with ease

Do you find yourself navigating frequently between local git branches? I put together this bash function that allows you to checkout to a branch using a simple wildcard search.

Syntax: br {any_word} where any_word is a part of a branch name. So if I have a branch called "honey nut cheerios", I could type br cheerios and then my terminal would checkout to that branch.

If there are multiple matches, it just shows all matches onscreen. Then you can switch manually.

Hope you find it useful!

function br {
    OUTPUT=$(git branch --list *$1*;)
    if (( $(grep -c . <<<"$OUTPUT") > 1 )); then
      echo Multiple branches found:
      echo "${OUTPUT}"
    else
      TRIMMED_OUTPUT=$(echo "${OUTPUT}" | xargs)
      echo "Switching to:${TRIMMED_OUTPUT}"
      git checkout "${TRIMMED_OUTPUT}"
    fi
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
yoniweisbrod
Yoni Weisbrod

Posted on October 27, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related