Skip to content

Instantly share code, notes, and snippets.

@peterjwest
Last active July 7, 2023 15:42
Show Gist options
  • Save peterjwest/80fd15fceb401bc1cf04 to your computer and use it in GitHub Desktop.
Save peterjwest/80fd15fceb401bc1cf04 to your computer and use it in GitHub Desktop.
Git aliases
#!/usr/bin/env bash
set -euo pipefail
for BRANCH in $(git branch | grep -E -v "^(\* | (develop|master|main)$)"); do
if [[ -z $(git --no-pager log develop..$BRANCH --author=$(git config user.email)) ]]; then
git branch -D $BRANCH
fi
done

git pushup - Pushes a branch and sets the upstream to be the branch of the same name (useful if your git push.default is simple):

git config --global alias.pushup \!'git push --set-upstream origin `git symbolic-ref --short HEAD`'

git trim - Prunes branches which are merged or have been deleted on the remote (never deletes develop or master):

git config --global alias.trim \!'git fetch --prune && git branch --merged | grep -E -v "^((\* )|\s*(develop|master)$)" | xargs git branch -d'

git pullup - Pulls a branch only if it can be fast forwarded, branch doesn't have to be checked out

git config --global alias.pullup \!'f() { BRANCH=$(git symbolic-ref --short HEAD); if [ $# -eq 0 ] || [[ $BRANCH == $1 ]]; then git pull --ff-only; else git fetch origin $1:$1; fi; }; f'

Others:

pushup = !git push --set-upstream origin `git symbolic-ref --short HEAD`
trim = !git fetch --prune && git branch --merged | grep -E -v \"^((\\* )|\\s*(develop|master|main)$)\" | xargs git branch -d
pullup = "!f() { BRANCH=$(git symbolic-ref --short HEAD); if [ $# -eq 0 ] || [[ $BRANCH == $1 ]]; then git pull --ff-only; else git fetch origin $1:$1; fi; }; f"
restash = "!f() { NAME=$(git stash list | head -n 1 | sed 's/.*\\: //'); git stash pop; git stash save ${@:-$NAME}; }; f"
destash = "!f() { NAME=$(git stash list | grep \": $1$\" | cut -d: -f1); if [[ ! -z $NAME ]]; then git stash apply $NAME; else echo \"Stash not found '$1' \"; git --no-pager stash list; fi; }; f"
rebranch = "!f() { BRANCH=$(git symbolic-ref --short HEAD); if [ $# -eq 0 ] || [[ $BRANCH == $1 ]]; then git reset --hard origin/$BRANCH; else git branch -f $1 origin/$1; fi; }; f"
uncommit = !git reset HEAD~1
cleanup = "!f() { ~/git-cleanup.sh; }; f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment