Some Git sugar for your .bashrc

I'm one of those people that use Git command line quite a lot and have not really adopted any Git GUI clients. To make make life in the terminal more efficient I have added some sugar to my .bashrc.

Make "g" an alias for "git"

alias g='git'
__git_complete g __git_main

The first line creates the alias g for git. Sadly tab completion stops working and the second line takes care of re-adding that. At the time of writing I'm running Git version 2.11.0 on macOS installed via Brew.

Branch Shortcuts

alias bm='git checkout master'
alias bd='git checkout develop'
alias bb='git checkout -'

alias b='git checkout'
__git_complete b _git_checkout

alias bn='git checkout -b'
__git_complete bn _git_checkout

function bs() {
    local branch="$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')"

    if [[ "${branch}" == "" ]] ; then
        echo "No branch found!"
        return 1

    echo "${branch}" > ~/savedbranch.txt

function bl() {
    local branch="$(cat ~/savedbranch.txt)"
    git checkout "${branch}"

Jumping between branches is so common I've added aliases that even allow me to omit the initial g. The functions bs and bl stands for "branch save" and "branch load" and allow me to save a certain branch name that I can later on jump back to.

Gitty Git Aliases

# Clone
git config --global 'clone'
# Branch
git config --global alias.b 'branch'
git config --global 'branch'
git config --global alias.bdl 'branch --delete'
git config --global alias.bdr 'push origin --delete'
git config --global '!f() { read -r -p "Delete the branch ${1} both locally and remotely? [y/N]" response; if [[ "${response}" =~ ^([yY][eE][sS]|[yY])$ ]]; then echo "... deleting local branch ${1} ..."; git branch --delete "${1}"; echo "... deleting remote branch ${1} ..."; git push origin --delete "${1}"; echo "... done."; else echo "aborted"; fi }; f'
# Check Out
git config --global 'checkout'
# Status
git config --global alias.s 'status'
git config --global alias.d 'diff'
# Add
git config --global alias.a 'add'
git config --global alias.aa 'add --all'
# Commit
git config --global alias.c 'commit'
git config --global 'commit'
git config --global 'commit --amend'
git config --global alias.cia 'commit --amend'
# Merge
git config --global alias.m 'merge'
# External Tools
git config --global 'mergetool'
git config --global alias.dt 'difftool'
# Pull
# Pushing
git config --global alias.p 'push'
git config --global 'push --force'
# Fetching
git config --global alias.f 'fetch'
git config --global alias.fa 'fetch --all'
# Reset
git config --global alias.r 'reset'
git config --global alias.r1 'reset HEAD^'
git config --global alias.r2 'reset HEAD^^'
git config --global alias.rh 'reset --hard'
git config --global alias.rh1 'reset HEAD^ --hard'
git config --global alias.rh2 'reset HEAD^^ --hard'
# Cherry Pick
git config --global alias.cp 'cherry-pick'
# Stash
git config --global 'stash list'
git config --global 'stash apply'
git config --global 'stash save'

Of course you should throw in some regular "git git aliases" as well.

Bulk Actions on Many Repositories

# Buffer and Eachdir
buffer ()
    local buffer="$(cat; echo 'x')"
    printf '%s' "${buffer%x}"
eachdir ()
    local cmd="${@}"
    local directories=($(find . -mindepth 1 -maxdepth 1 -type d))
    for directory in "${directories[@]}" ; do
        local directory="${directory:2}"
        {(cd "${directory}"; echo -e "\e[92m[ ${directory} ]\e[0m"; eval "${cmd}") 2>&1 | buffer &} 2>/dev/null
    wait 2>/dev/null

Behold mein bulk action tool! Assume you wanted to git add everything in all repositories placed within the directory you are currently standing. This is how you would do it:

eachdir 'git add --all'
eachdir 'git commit -m "I made this same change to many repositories"'
eachdir 'git push'

And the beauty of it is that the work is done asynchronously.