Skip to content

Instantly share code, notes, and snippets.

@fritz-c
Forked from jordan-brough/git-recent
Last active June 7, 2024 17:41
Show Gist options
  • Save fritz-c/c1e528b09bc1c0827a3c to your computer and use it in GitHub Desktop.
Save fritz-c/c1e528b09bc1c0827a3c to your computer and use it in GitHub Desktop.
Git: Check out a branch from a list of recently checked out branches/tags/commits
#!/usr/bin/env bash
# Source: https://gist.github.com/fritz-c/c1e528b09bc1c0827a3c
# Original: https://gist.github.com/jordan-brough/48e2803c0ffa6dc2e0bd
# Download this script as "git-recentco" (no extension), chmod it to be executable and put it in your
# path somewhere (e.g. /usr/bin). You can then use it via `git recentco` from inside any git repo.
# Example:
#
# $ git recentco -n 5
# 1) master
# 2) stable
# 3) develop
# 4) some-cool-feature
# 5) feature/improve-everything
# Choose a branch: 3
# Switched to branch 'develop'
usage()
{
echo "usage: git recentco [-n lines]"
}
while getopts "hn:" opt; do
case $opt in
h)
usage
exit 1
;;
n)
NUM=$OPTARG
;;
\?)
usage
exit 1
;;
esac
done
NUM=${NUM-10} # default to 10 lines
# This: `awk ' !x[$0]++'` removes duplicates. See http://stackoverflow.com/questions/11532157
UNIQUE_BRANCHES=$(git reflog | egrep -io "moving from ([^[:space:]]+)" | awk '{ print $3 }' | awk ' !x[$0]++')
# Exclude branches that don't exist locally
BRANCH_CHOICES=( $(echo "$UNIQUE_BRANCHES" | while read line; do
git rev-parse --verify "$line" &>/dev/null && echo "$line"
done | head -n "$NUM") )
PS3="Choose a branch: "
select d in "${BRANCH_CHOICES[@]}"; do test -n "$d" && break; echo ">>> Invalid Selection"; done
git checkout "$d"
@aalvarado
Copy link

What's the difference between this one and the original?

@fritz-c
Copy link
Author

fritz-c commented Jun 26, 2019

@aalvarado All the code from
# Exclude branches that don't exist locally onward.
The original just lists them; this filters down to only local branches and offers the choice to check them out.

@aalvarado
Copy link

ah great, thank you

@jordan-brough
Copy link

Thanks @fritz-c! I've updated my script to incorporate that idea and I've listed you as a contributor in the script.
Lmk if you have any thoughts/suggestions.

@cool-RR
Copy link

cool-RR commented Apr 14, 2020

@fritz-c Would you be okay with officially releasing your contribution to the script as open-source? I've asked @jordan-brough separately to release it under the MIT license.

@fritz-c
Copy link
Author

fritz-c commented Apr 14, 2020

@cool-RR yes, I’m fine with making it open source. I don’t need credit or anything.

@fritz-c
Copy link
Author

fritz-c commented Feb 10, 2022

@jordan-brough Just noticed your comment a couple days late 😅 I checked out your updated script and it looks great. Thank you for including me in the contributors.

@vorpal56
Copy link

vorpal56 commented Jun 7, 2024

fork to show in 1 column in select and handle 'q' option for quitting https://gist.github.com/vorpal56/fd4a688a7ed6a84ed1716b0095ff1da6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment