Skip to content

Instantly share code, notes, and snippets.

@Ragnoroct
Last active March 30, 2023 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ragnoroct/ffde5aa9143b850eecd5329990ac8fdb to your computer and use it in GitHub Desktop.
Save Ragnoroct/ffde5aa9143b850eecd5329990ac8fdb to your computer and use it in GitHub Desktop.
Another git recent command to see your recently checked out files. It can also switch if you give a number argument.
#!/usr/bin/env bash
# Copyright (c) 2023 Will Bender. All rights reserved.
#
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
# See most recently checked out branches and easily switch to one
NUMBER=5 # Max branches to echo in list
OPTS=$(getopt -o n:h --long help -- "$@") || { echo "Failed parsing options." >&2; exit 1; }
eval set -- "$OPTS"
while true; do
case "$1" in
-h | --help)
echo "Usage: git recent [branchNum] [-n listLength] [-h]"
echo "git recent Lists last $NUMBER checked out branches"
echo "git recent -n 10 Lists last 10 checked out branches"
echo "git recent 2 Checks out the 2nd most recent branch"
exit ;;
-n) NUMBER=$2; shift 2; ;;
--) shift; break; ;;
*) break ;;
esac
done
current_branch="$(git rev-parse --abbrev-ref HEAD)"
function recent_cmd() {
git reflog | grep -Eio 'moving from ([^[:space:]]+)' | awk '{ print $3 }' | awk ' !x[$0]++' |
grep -Ev '^[a-f0-9]{40}$' | grep -Ev '^master$' | grep -Ev "^$current_branch$" | head -n "$NUMBER" | awk '{print NR " " $s}'
}
if [ -z "$1" ]; then
#print currently checked out branch
printf "* \e[0;32m%s\e[0m\n" "$current_branch"
recent_cmd
else
#checkout branch from list
recent_cmd | grep -Po "(?<=^$1 ).*" | xargs git checkout
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment