Skip to content

Instantly share code, notes, and snippets.

@serrasqueiro
Last active January 5, 2026 12:58
Show Gist options
  • Select an option

  • Save serrasqueiro/4a268047eb5ffb4991ef0c4233aad552 to your computer and use it in GitHub Desktop.

Select an option

Save serrasqueiro/4a268047eb5ffb4991ef0c4233aad552 to your computer and use it in GitHub Desktop.
Bash sourced script
# .alias_addons-2 -- H.Moreira
function short_git_list ()
{
git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(refname:short)'
return $?
}
function git_checkout ()
{
local N
local RES=0
local STR
echo -n "# " ; git remote get-url origin 2> /dev/null
RES=$?
echo
[ $RES != 0 ] && return 2
if [ "$*" = "." ]; then
git checkout master
return $?
fi
#git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:blue)%(refname:short)%(color:reset) - %(committerdate:relative)'
short_git_list
[ $? != 0 ] && return 1
[ "$*" = "" ] && return 0
echo "# ----"
N=$(short_git_list | grep -i "$*" | wc -l | sed 's/[ ]*//g')
case $N in
1)
# single match
echo git checkout $(short_git_list | grep -i "$*" | tr -d '*')
git checkout $(short_git_list | grep -i "$*" | tr -d '*')
return $?
;;
0)
git checkout $*
RES=$?
;;
*)
echo "Found too many: $*"
;;
esac
[ $RES = 0 ] && return 0
git branch -a | grep -q "$*"
[ $? != 0 ] && return 2
echo -n "Err... did you mean... ?
"
git branch --list -a | grep -c "$@"
N=$(git branch --list -a | grep -c "$@")
if [ "$N" = 1 ]; then
STR=$(git branch --list -a | grep "$@" | | sed -E 's#^remotes/[^/]*/##; s#^origin/##' | tr -d ' ')
if [ "$STR" ]; then
echo "Found $N: $STR"
git checkout "$STR"
return $?
fi
fi
git branch --all | sed 's/ remotes\// /' | grep "$*"
return $?
}
alias co=git_checkout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment