Skip to content

Commit

Permalink
git recentco (recent references alias)
Browse files Browse the repository at this point in the history
  • Loading branch information
fbartho committed Aug 20, 2019
1 parent d0c6f3b commit b94a90f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitconfig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
split = !git-split
showlocal = !git-showlocal
prunelocal = !git-prunelocal
recent = !git-recentco
recentco = !git-recentco
reauthor = !sh -c 'eval `git log --reverse --topo-order --pretty=format:\"git cherry-pick %H && git commit --amend -C %H --author=\\\"%aN <%aE>\\\" && \" $0 ` "echo success" '
stash-rename = "!_() { rev=$(git rev-parse $1) && git stash drop $1 || exit 1 ; git stash store -m \"$2\" $rev; }; _"
[color]
Expand Down
54 changes: 54 additions & 0 deletions bin/git-recentco
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/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"

0 comments on commit b94a90f

Please sign in to comment.