How git aliases can save you hours of time

Imagine a coding world where you can navigate your Git repository, commit changes, and handle merges and branches at lightning speed.

What are aliases

Aliases are a faster way to execute a command. They are shortcuts for longer commands. Imagine typing every time a command like git push origin master. You can type it 999 times, but by the 1000th time you feel tired. What if you can just use initials?

gpom (git push origin master)

Wouldn’t that be great? Aliases let you just do that. Personally, I make use of two distinct types of aliases:

Here are some aliases I use daily1:

gss

Stands for git status short. It lets me see the status of my repository at a glance

alias gss="git status --short"

git status short

gshsm

Stands for git stash message (letters are swapped for faster typing). It lets me create a new stash with a custom message

alias gshsm="git stash -m"

gshsl

Stands for git stash list (letters are swapped for faster typing). It lets me list all saved stashes

alias gshsl="git stash list"

gls

Stands for git ls (or git list). It works like the standard ls, but for repos. It lets me list all files in a specific branch (or commit). I usually combine it with grep

alias gls="git ls-tree -r --name-only"

git list

glg

Stands for git log global (or git log graph, I can’t decide). It shows me commit graph for all branches

glg () {
    git log \
        --format=format:"%C(bold yellow)%h%C(auto)%d ~ %C(blue)[%cD]%C(reset)%n    %C(white)%s%C(reset)%C(dim white) • %an%C(reset)%C(green)" \
        --all \
        --graph \
        --abbrev-commit \
        --decorate \
        --topo-order \
        --date-order
}

How it looks like: git log global

glf2

Stands for git log fuzzy-finder3. A commit search-engine. I can filter commit by commit message, and see a preview on the right. If I hit enter I can open the full diff

_gitLogLineToHash="echo {} | grep -o '[a-f0-9]\{7\}' | head -1"
_diffrCommand="diffr \
    --line-numbers \
    --colors refine-added:none:background:51,153,51:bold \
    --colors added:none:background:51,85,51 \
    --colors refine-removed:none:background:191,97,106:bold \
    --colors removed:none:background:85,51,51"
_viewGitLogLine="$_gitLogLineToHash | xargs -I % sh -c 'git show -p --color=always %'"

glf () {
    git log \
        --color=always \
        --format="%C(bold yellow)%h %C(reset)%s %C(reset)%C(blue)[%cr]" "$@" |
        fzf -i -e \
        --no-sort \
        --reverse \
        --tiebreak=index \
        --no-multi \
        --ansi \
        --header="control-y: copy hash" \
        --preview="git show -p --color=always {1} | $_diffrCommand | less -R" \
        --bind="enter:execute:$_viewGitLogLine | $_diffrCommand | less -R" \
        --bind="ctrl-y:execute:$_gitLogLineToHash | pbcopy" 
}

git log fuzzy

git lost-stashes

This is self-explanatory. It lets you find stashes that you deleted or accidentally dropped (yeah, you can do it)

# ~/.gitconfig
[alias]
    lost-stashes = !LANG=en_GB git fsck \
        --unreachable \
        --no-reflog \
        | grep commit \
        | cut -d ' ' -f3 \
        | xargs git log \
            --merges \
            --no-walk

Simplify your workflow, save time, and maximize productivity with these handy shortcuts. Happy coding!


  1. The repository shown is my APDynamicGrid ↩︎

  2. I use diffr, a diff highlighting tool ↩︎

  3. fzf is a powerful command-line finder ↩︎