Skip to content

Instantly share code, notes, and snippets.

@MattMcMurray
Forked from jordan-brough/git-recent
Created August 10, 2023 20:07
Show Gist options
  • Save MattMcMurray/a8a53e0833192c38dd776963aaef7cf9 to your computer and use it in GitHub Desktop.
Save MattMcMurray/a8a53e0833192c38dd776963aaef7cf9 to your computer and use it in GitHub Desktop.
Git: Display a list of recently checked out branches/tags/commits
#!/usr/bin/env bash
# Source: https://gist.github.com/jordan-brough/48e2803c0ffa6dc2e0bd
# See also: https://stackoverflow.com/a/25095062/58876
# Download this script as "git-recent" (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 recent` from inside any git repo.
# Examples:
# Interactive prompt for most recent 4 branches:
# $ git recent -n 5
# 1) master 4) deleted-branch
# 2) stable 5) improve-everything
# 3) fun
# Choose a branch: 2
# List mode (non-interactive):
# $ git recent -n 5 -l
# master
# stable
# fun
# deleted-branch
# improve-everything
# Verify branch existence:
# $ git recent -n 5 -l -e
# master
# stable
# fun
# improve-everything
# something-else
# # (notice "deleted-branch" removed from results)
# Contributors:
# - jordan-brough
# - fritz-c
usage()
{
echo "usage: git recent [-n lines] [-l] [-e]"
}
while getopts "hn:le" opt; do
case $opt in
h) # help
usage
exit 1
;;
n) # number of results
NUM=$OPTARG
;;
l) # list mode (non-interactive)
INTERACTIVE=0
;;
e) # validate existence
CHECK_EXISTENCE=1
;;
\?)
usage
exit 1
;;
esac
done
NUM=${NUM-10} # default to 10 lines
INTERACTIVE=${INTERACTIVE-1} # default to interactive
CHECK_EXISTENCE=${CHECK_EXISTENCE-0} # default to *not* checking existence (faster)
BRANCHES=(
$(git reflog |
egrep -io "moving from ([^[:space:]]+)" |
awk '{ print $3 }' | # extract 3rd column
awk ' !x[$0]++' | # Removes duplicates. See http://stackoverflow.com/questions/11532157
egrep -v '^[a-f0-9]{40}$' | # remove hash results
while read line; do # verify existence
([[ $CHECK_EXISTENCE = '0' ]] || git rev-parse --verify "$line" &>/dev/null) && echo "$line"
done |
head -n "$NUM"
)
)
if [[ $INTERACTIVE = '1' ]]; then
PS3="Choose a branch: "
select d in "${BRANCHES[@]}"; do
test -n "$d" && break;
echo ">>> Invalid Selection";
done
git checkout "$d"
else
printf '%s\n' "${BRANCHES[@]}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment