Skip to content

Instantly share code, notes, and snippets.

@MatthewBooth
Last active February 23, 2019 09:22
Show Gist options
  • Save MatthewBooth/d5a300a38c5ae820ac6b71bf2d8ce254 to your computer and use it in GitHub Desktop.
Save MatthewBooth/d5a300a38c5ae820ac6b71bf2d8ce254 to your computer and use it in GitHub Desktop.
Useful Git Aliases

Useful Git Aliases

These are some very useful Git aliases that I've found to be invaluable when using Git on a day-to-day basis.

Remove merged branches

This will remove any branch from your local repo that has been merged in the remote. Useful for keeping your local copy clean.

Put this into your .bashrc file or .bash_aliases:

alias git-rm-merged='git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d'

Remove orphaned branches

This will remove any local branches that no longer exist on the remote, and has been merged.

Put this into your .bashrc file or .bash_aliases:

alias git-rm-orphaned="git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d"

You may also change to this method, which will force remove any branch from your local repo that isn't on the remote.

Use with caution!

This could delete any branches you could be working on:

alias git-rm-orphaned-force="git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -D"

Keeping a repo up to date

This is a Python port of https://github.com/aanand/git-up, which still works perfectly well, but is no longer maintained. This will stash, pull and rebase your local branches in line with their state on the remote.

Run the following commands from a terminal:

sudo -H pip3 install git-up

git config --global alias.up git-up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment