DEV Community

Kara Luton
Kara Luton

Posted on • Edited on

Aliases: Making the command line your friend

How many times a day do you write git add, git commit -m and git push in your terminal? Tons! Wouldn't it be nice if there was a faster way to write out the commands you use every day? Well, there is and it's aliases.

I'll be going over using aliases with Zsh which is now the default shell for Macs using Catalina. Zsh is an extended version of Bash but it has a few additional features and has support for plugins and themes.

Making an Alias

You'll need to open your Zsh configuration file (.zshrc) and add the following syntax at the bottom of the file to make your alias:

alias [custom-alias]=["command"]

I use Ember at work so I'm constantly writing out the full command to run my tests. Instead of having to write ember test --server --launch=false every time I've set up the following alias:

alias ets="ember test --server --launch=false"

Now, whenever I want to run my tests I use the command ets and have saved myself a little bit of time typing!

Oh My Zsh

Another advantage of using Zsh as your shell is that you can install Oh My Zsh which is a framework for managing your Zsh configuration.

You can install Oh My Zsh via curl:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Besides having awesome plugins and themes, Oh My Zsh also includes a ton of git aliases. You can see which ones are available by running alias. There are a ton of preconfigured aliases and all you have to do is add them to your .zshrc configuration file.


What's your favorite Git alias? I'd love to know in the comments below!

Be sure to follow me on Twitter for lots of posts about tech, and if I'm being honest, lots of posts about dogs too.

Top comments (9)

Collapse
 
dak425 profile image
Donald Feury • Edited

I made a couple recently while working on a Rails project.

# reload zsh config
alias reload!='RELOAD=1 source $HOME/.zshrc'

# rails
alias rgs='rails g scaffold'
alias rgmo='rails g model'
alias rgmi='rails g migration'
alias rgv='rails g view'
alias rgj='rails g job'

# rake/rails
alias rdbm='rake db:migrate'

# tmux
alias ta='tmux a'
alias tls='tmux ls'
alias tat='tmux a -t'
alias tns='tmux new -s'
alias tsync='tmux setw synchronize-panes'

I have the reload alias because I find myself tweaking my dotfiles frequently while working.

Collapse
 
twwilliams profile image
Tommy Williams

Inspired by Oh My Zsh, there's Oh My Posh for PowerShell. It focuses only on command-line theming (no Git aliases).

github.com/JanDeDobbeleer/oh-my-posh

Combined with posh-git, it provides a very nice experience for Windows users with PowerShell.

Collapse
 
tsjost profile image
tsjost

I can't live without being able to entirely clear the terminal and its scrollback buffer:

alias cls='printf "\033c"'

Checking what directories and files are eating your precious disk space is invaluable too, and sorting the output (for GNU, not BSD/Mac):

alias duf='du -skh .[!.]* * | sort -h'

No need to keep typing git all the time when you can just:

alias g=git

My long list of git aliases for extreme efficiency, most of which I use daily:

logg = log --graph --decorate --oneline
lgg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ar)' --abbrev-commit --date=relative
l = !git lg HEAD ^master
sb = status -sb
a = add
ap = add -p
b = branch
d = diff
dc = diff --cached
unstash = !git stash show -p | git apply -3 && git stash drop
c = commit
co = checkout
ca = commit --amend
r = rebase
rc = rebase --continue
ri = rebase -i
ra = rebase --abort
m = merge
ma = merge --abort
s = show
st = stash
ss = stash show -p
sp = stash pop

And when you're feeling too lazy to write proper commit messages:

whatever = !git commit -m\"`curl -s https://whatthecommit.com/index.txt`\"

(don't actually do this 😛)

Collapse
 
joekaiser profile image
Joe • Edited

This one formats the git log to be more concise: ls = git log --pretty=format:"%ci\\ %C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate

2019-11-08 15:41:44 -0800 de01370 add dev warning to admin config [Joe Kaiser]
2019-11-07 15:36:34 -0800 eab0f32 sort configItems after search [Joe Kaiser]
2019-11-06 22:18:59 -0800 ea7cf65 new/delete config items [Joe Kaiser]
2019-11-06 21:29:29 -0800 aafafb2 admins can update config items [Joe Kaiser]
2019-11-05 17:29:16 -0800 a21a51e admin user pages [Joe Kaiser]

Delete all branches that were once part of origin, but are no longere there. Any local branches you have made but not pushed will be safe. prune-branches = git remote prune origin && git branch -vv | grep ': gone]' | awk '{print $1}' | xargs -r git branch -D

$> git prune-branches
Pruning origin
 * [pruned] origin/emailTrack
 * [pruned] origin/failedPause
 * [pruned] origin/fullheight
 * [pruned] origin/inboxui
Collapse
 
zakmiller profile image
Zak Miller

What I use for git:

alias gs "git status"
alias ga "git add"
alias gc "git commit"
alias gp "git push"
alias gpl "git pull"
alias gch "git checkout"
alias gt "git add -u && git commit --amend --no-edit && git push -f"
alias gl "git log"

For hugo:

alias hs "hugo server --disableFastRender --watch -v"

To generate a UUID (universally unique identifier) quickly:

alias uuid="python -c 'import sys,uuid; sys.stdout.write(uuid.uuid4().hex)' | pbcopy && pbpaste && echo"

To do an ls every time I do a cd:

function cd
    if count $argv > /dev/null
        builtin cd "$argv"; and ls
    else
        builtin cd ~; and ls
    end
end
Collapse
 
richardeschloss profile image
Richard Schloss

gpod: git pull origin development
grod: git rebase -i development (grod as in "Git Rebase On top of Development"; the "o" is for "on")
(because when working on I team, I always have to remember to get current first...that's easy to forget)

Collapse
 
timbeaudet profile image
🏎️Tim Beaudet🚀

Not super original but I like:

git cm (commit --message)
git pr (pull --rebase)
git ss (stash --save)
git lg (git --log --oneline ???) and other nice options.

Collapse
 
jmplourde profile image
Jean-Michel Plourde

my favorite zsh alias is gpsup for git push --set-upstream

Collapse
 
filix profile image
Filip Ilić

After reading this I was interested how to make alias with variable, so I googled it and find out you can use functions since aliases do not support variable.

Put this inside function in your .bashrc or .zshrc file:

mtd () { mv "$@" /path/to/documents/; }

To move file.md to documents just use:

 file.md 

Or... you can put the function in an alias:

alias mtd='mvtd () { mv "$@" /path/to/documents/; }; mvtd;'