From bbb3231d7884bf194a7c0e4c0b8e4b7b77e14e4f Mon Sep 17 00:00:00 2001 From: jc <46619361+juancwu@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:10:33 -0500 Subject: [PATCH] add functions to quickly find and switch branches --- .config/bash/.bashrc | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/.config/bash/.bashrc b/.config/bash/.bashrc index 41a81f4..98c2122 100644 --- a/.config/bash/.bashrc +++ b/.config/bash/.bashrc @@ -166,6 +166,28 @@ git-prune() { git branch -vv | grep '\[origin/.*: gone\]' | awk '{print $1}' | xargs git branch -d } +# fuzzy find branches and switch to selected branch +gco() { + local selected_branch=$(git branch | fzf | sed 's/^[ \*]*//') + + if [ -n "$selected_branch" ]; then + git checkout "$selected_branch" + else + echo "No branch selected" + fi +} + +# fuzzy find remote branches and switch to selected branch +gcr() { + local selected_branch=$(git branch -r | fzf | sed 's/^([ \*]*origin\/[\ *]*)*//') + + if [ -n "$selected_branch" ]; then + git checkout "$selected_branch" + else + echo "No branch selected" + fi +} + # Set prompt if [ "$color_prompt" = yes ]; then PS1="${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]\$(parse-git-branch)\[\033[00m\] \$ "