Barbarian Meets Coding
barbarianmeetscoding

WebDev, UX & a Pinch of Fantasy

3 minutes readgit

Barbaric Tip of the Week: Be More Productive and Git Things Done With These Git Aliases

Barbaric Tip of the Week is a weekly series whose main purpose is to share tiny bits of knowledge that I find specially useful and interesting.

I think it was Scott Hanselman who said that you only have so many keystrokes in a lifetime. Yep… That sentence right there was 145 less keystrokes in my personal account. 145 keystrokes that are never coming back…

That’s why it is so important to find ways to make your keystrokes count. You can write a blog and make your keystrokes extra worthy by reaching as many readers as you can. You can become a touch typing ninja, increase your WPM (words per minute) and enjoy a longer keystroke-lifespan. Or you can grok the tools you use daily and achieve more with less keystrokes.

Enter Git Aliases!!!

Git aliases let you define aliases for git commands you use frequently so that you can do more with less typing. For instance, aliases let you commit by typing git ci instead of git commit or look at your beautified logs by using git lol instead of git log --graph --decorate --pretty etc....

You can create an alias by using the git config subcommand:

> git config --global alias.aliasname 'command to execute when using the alias'

# you prepend a ! if you want git to run an external command
> git config --global alias.v '!vim'

Here is a list of my favorite aliases in no particular order. Enjoy!

Common aliases

> git config --global alias.st status     
> git config --global alias.ci commit
> git config --global alias.co checkout
> git config --global alias.cb 'checkout -b'
> git config --global alias.br branch

Cleaning

> git config --global alias.cf 'clean -f'           # clean force
> git config --global alias.cs 'checkout -- .'      # discard changes in working directory
> git config --global alias.unstage 'reset HEAD --' # discard changes in staging area
> git config --global alias.us 'reset HEAD --'             

# Clean up your working directory with flare
> git config --global alias.obliterate '!git clean -fd && git checkout -- . && echo "Everything has been obliterated, oh great master of evil and deceit!"'

Rebasing

> git config --global alias.reb rebase
> git config --global alias.rbi 'rebase -i'

Logs

> git config --global alias.lol "log --graph --decorate --pretty=oneline --abbrev-commit --all"  # nice logs
> git config --global alias.l log
> git config --global alias.lone 'log -1'
> git config --global alias.last 'log -1'
> git config --global alias.hist 'log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short' # more nice logs
> git config --global alias.gk '!gitk'

Diffs

> git config --global alias.d difftool

Stashing

> git config --global alias.s stash
> git config --global alias.sl 'stash list'
> git config --global alias.laststashed 'stash apply stash@{0}'    # apply last stashed item without removing from the stash
> git config --global alias.slast stash 'apply stash@{0}'          # same as above

Configurations

> git config --global alias.cg 'config --global'
> git config --global alias.cgl 'config --global --list'

And that is that. Hone your skills. Know thy tools. Make the most of those precious keystrokes!

P.S. Do you have any favorite aliases yourself?


Jaime González García

Written by Jaime González García , dad, husband, software engineer, ux designer, amateur pixel artist, tinkerer and master of the arcane arts. You can also find him on Twitter jabbering about random stuff.Jaime González García