Skip to content

Instantly share code, notes, and snippets.

@holtbp
Last active September 27, 2016 20:50
Show Gist options
  • Save holtbp/5bea10d01ff08630b9bf to your computer and use it in GitHub Desktop.
Save holtbp/5bea10d01ff08630b9bf to your computer and use it in GitHub Desktop.
Git Helpers
#!/bin/bash
# Original by Gary Bernhardt:
#
# https://raw.github.com/garybernhardt/dotfiles/master/.githelpers
#
# Log output:
#
# 51c333e 2012-07-12 Gary Bernhardt add vim-eunuch
#
# The log format uses } characters between each field, and `column` is later
# used to split on them. A } in the commit subject or any other field will
# break this.
HASH="%C(yellow)%h%Creset"
DATE="%C(green)%ad%Creset"
AUTHOR="%C(bold blue)%an%Creset"
REFS="%C(red)%d%Creset"
SUBJECT="%s"
FORMAT="$HASH}$DATE}$AUTHOR}$REFS $SUBJECT"
show_git_head() {
echo "$(pretty_git_log -1)
$(git show --color -p --pretty="tformat:")" |
less -FXRS
}
pretty_git_log() {
git log --pretty="tformat:${FORMAT}" --date=short $* |
# Line columns up based on } delimiter
column -s '}' -t |
# Page only if we need to
less -FXRS
}
fixup() {
fixup_or_squash fixup
}
squash() {
fixup_or_squash squash
}
fixup_or_squash() {
git commit -am "$1! $(git log -1 --format='%s' $@)"
}
prune_merged() {
# TODO: Test for origin/master vs. master instead.
test -n "$1" && prune_merged_remotes $@ || prune_merged_local
}
prune_merged_local() {
git branch --list --merged |
grep --invert-match --extended-regexp '\*|master' |
xargs -n 1 git branch --delete
}
prune_merged_remotes() {
git fetch origin
git remote prune origin
git branch --list --remote --merged $1 |
grep --invert-match --extended-regexp "HEAD|master|$1" |
sed 's/ *origin\///' |
xargs -p -I branch git push origin :branch
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment