DEV Community

Micheal Ojemoron
Micheal Ojemoron

Posted on • Updated on

Productivity 101: Git Aliases for Lazy Developers

According to Larry Wall, the original author of the Perl programming language, one of the qualities of a great program is laziness...
"The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful, and document what you wrote so you don’t have to answer so many questions about it. Hence, the first great virtue of a programmer."

Some years ago, I stumbled upon an interesting article- Eliminate Stupid Mental Effort (ESME). I can't remember the author's name. The article explains why some developers are unproductive because they spend too much time on repetitive tasks that can easily be avoided.

As a lazy developer, I started using aliases in my git workflow to save myself from typing long commands. So instead of typing "git checkout master", my lazy self simply types "gom", which saves precious human fingertip power by allowing the shorter keystroke form.

What are aliases?
Aliases are used to create shorter commands that map to longer commands.
They are commonly found in popular utilities like bash.

How to create aliases
Creating an alias is as simple as typing:

alias ALIAS_NAME="ALIAS_COMMAND" # generic form
alias gom = "git checkout master" # git example
Enter fullscreen mode Exit fullscreen mode

To get started, make sure you have installed git bash or any bash program on your system.
Open git bash, and type "cd" to get to your home directory.
Type the following commands to create a .bashrc file that automatically runs whenever you open git bash.

touch .bashrc # create a  .bashrc
Enter fullscreen mode Exit fullscreen mode

You can either open the .bashrc file with any code editor to start adding your aliases or use shell output redirection.

Using Output Redirection
For example, let's add an alias for "git rebase" command

cd ~ # to enter your home directory
echo alias gre=\"git rebase\" >> .bashrc
Enter fullscreen mode Exit fullscreen mode

To have this change taken into account you should then either source this file (ie: run source .bashrc) or restart your terminal.

Common examples of lazy git aliases

alias gs="git status" # to see changes that have been staged and which haven't
alias gac="git add . && git commit -m" # to stage and commit changes
#git push and pull
alias gp="git push" # + remote & branch names
alias gl="git pull" # + remote & branch names

# Pushing/pulling to origin remote
alias gpo="git push origin" # + branch name
alias glo="git pull origin" # + branch name

# Pushing/pulling to origin remote, master branch
alias gpom="git push origin master"
alias glom="git pull origin master"

alias gcb="git checkout -b" # To create a new branch and checkout into it
alias go="git checkout"
alias gom="git checkout master"
alias gre="git rebase"

alias gd='git diff' 
alias glo="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"


Enter fullscreen mode Exit fullscreen mode

In conclusion, using git aliases in your git workflow will increase your productivity as an Engineer and eliminate stupid mental effort.

Also, note that aliases are not only meant for git workflow, you can also create aliases for any command you use frequently e.g Docker, Laravel, Ruby, Python, WordPress and Django commands, etc.

What aliases are you using?

Kindly follow me and turn on your notification.
Thank you! Happy coding! ✌

Top comments (28)

Collapse
 
190245 profile image
Dave • Edited

Why put git aliases in the .bashrc file? This implies a specific shell, in a specific OS (or set of).

I have aliases in my .bashrc, but none of them are git related.

My git aliases are in my .gitconfig file.

Things like 'git acp "message"' - add, commit push, with a commit message. 'git cof ' to check out a particular file, from a particular branch.

Collapse
 
mojemoron profile image
Micheal Ojemoron

yeah, I understand the angle you are coming from. Uncoupling git aliases from the shell environment is a better way to go, thanks for the feedback. Nice git aliases :)

Collapse
 
190245 profile image
Dave

My full .gitconfig (sanitised), if you're interested. Steal/borrow whatever you feel might be useful.

pastebin.com/wPavbDnJ

(and yes, my Bash Aliases has "g=git", so I can do things like g acp "commit msg")

Thread Thread
 
mojemoron profile image
Micheal Ojemoron

Lol πŸ˜‚ Nicee! I will borrow some πŸ˜‰

Thread Thread
 
190245 profile image
Dave

Since sharing is caring, I also have the branch (and some identifiers) on my terminal prompt.

.bashrc:

function parse_git_branch() {
    BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
    if [ ! "${BRANCH}" == "" ]
    then
        STAT=`parse_git_dirty`
        echo " (${BRANCH}${STAT})"
    else
        echo ""
    fi
}
function parse_git_dirty {
    status=`git status 2>&1 | tee`
    dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
    untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
    ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
    newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
    renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
    deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
    bits=''
    if [ "${renamed}" == "0" ]; then
        bits=">${bits}"
    fi
    if [ "${ahead}" == "0" ]; then
        bits="*${bits}"
    fi
    if [ "${newfile}" == "0" ]; then
        bits="+${bits}"
    fi
    if [ "${untracked}" == "0" ]; then
        bits="?${bits}"
    fi
    if [ "${deleted}" == "0" ]; then
        bits="x${bits}"
    fi
    if [ "${dirty}" == "0" ]; then
        bits="!${bits}"
    fi
    if [ ! "${bits}" == "" ]; then
        echo " ${bits}"
    else
        echo ""
    fi
}

export PS1="[\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\]] $ "

Thread Thread
 
mojemoron profile image
Micheal Ojemoron

Wow, what does this script do?

Thread Thread
 
190245 profile image
Dave • Edited

Obviously don't add it if you don't understand it (never trust a random on the internet).

But to answer your question, this:
Terminal Image

On any terminal prompt in a git repository, I see username/hostname/path (as normal), but then the git branch (in yellow) followed by some "bit flags". In the picture, the exclamation mark tells me I have modified some file(s), the question mark tells me I've added some file(s). Just after a commit, the bit flags disappear to tell me that the working directory is "clean."

There's other flags, as described in the script. If I'm not in a git repo, I just see the normal terminal.

Collapse
 
ghost profile image
Ghost

strangely I have the opposite aproach, all my aliases are for non-productive stuff, like playing YT videos on certain resolutions; I prefer to use git and docker and alike on their original form, when you have to work on a different machine is annoying as hell otherwise. You get used to your nice git aliases and you'll became useless and/or really pissed really quick in another PC, it happened often when ssh and I'm not a fan to add unnecesary config files inside VM or containers.

Collapse
 
mojemoron profile image
Micheal Ojemoron • Edited

Wow, I never thought of this :)

Collapse
 
ghost profile image
Ghost

and think about it, when you are on your own machine, you are all happy and cozy, that's when you don't need to be pampered by nice aliases; when you are SSH-ing to a machine, on the other hand, you probably are fixing something, for some reason your automated deploy didn't take, some service don't want to stay up, maybe you lost some data, you're probably pissed, frustrated and annoyed, your coffee is getting cold, is late on a Friday and you want to go home; that's exactly when you don't want to be extra-annoyed by repeatedly typing twice because of your in-existent aliases. That's the kinda thing that makes keyboards go through monitors, that's when you need to have the commands fresh on your mind and your fingers properly trained. You wont need to remember how to pass youtube-dl parameters to mpv nor how to load dmenu all cute with fancy color, those can be aliased, Docker, Ansible and Git?, you definetely want to remember those by hart. At least that's my opinion :]

Thread Thread
 
190245 profile image
Dave

In my corporate life, we have chef that pushes user config around servers, things like my SSH key, .bashrc and .gitconfig are pushed to any server(s) I should have access to, and kept in sync (because it pulls from our personal git repos).

In my personal life, I do much the same, but without chef (git clone <url> && ./firstTimeSetup). It sets up a cronjob that runs daily to pull changes.

That's my approach to the "finger training" issue. Then I simply don't need to think, other than remembering to look at the hostname in the terminal prompt to see where I am.

Automate all the things, and forget about the fingers!

Thread Thread
 
ghost profile image
Ghost

that's a good point, in my case I do it with Ansible, but I don't have a "corporate" machine, I work with my own one and I don't want my personal files into machines that are now my own, I also have, on occasion, get into docker machines that don't even have bash, dash is much more limited and I don't want to deal with error messages and logs just because something in my config files depends on my setup and also when more than one persona have to work in a VM or container which bashrc you use?, yours or theirs? or you add more aliases? or you put more users on them? and more entries in visudo, etc. things get complicated really fast, for some triviality like to type dc instead of docker container seems too much trouble for me. I prefer to keep things simple for my brain, my fingers can use some little extra work and the sum of all the commands written in a day are probably like 5min of coding.

Thread Thread
 
190245 profile image
Dave

Without wishing to prescribe a working method, I/we do things a little differently.

As I'm always repeating to our Junior developers - "a good developer is a lazy developer" and "don't be WET" (WET = We Enjoy Typing, the opposite of DRY - Don't Repeat Yourself). Kind of ironically, I'm breaking DRY by repeating that to the Juniors...

As a result, I never need to connect to anything that doesn't have a full bash installation. Even Docker containers, the images are stripped bare, so don't have bash etc, and if something is wrong within the container, it's either in a volume or the image needs a rebuild. Same for VMs, if it's not a full blown Linux installation, I've no need to connect to it - and if I do need to connect to it, chef deals with sync for me.

FWIW, I also don't rely heavily on bash aliases - about the only two I can think of that I use with any regulatory are ll (which is just ls -latrh) and msg (which will send a message to a user on another TTY in the same host - e.g. msg bob "dw, I got this production issue, grab a coffee").

Thread Thread
 
ghost profile image
Ghost

well it depends on the situation, in my case sometimes I do have to login into machines without Bash and as I mentioned, I've typed more in this couple of comments than in a day of commands, I argue that in this case KISS is more relevant to me, to avoid a couple of key-presses is just irrelevant to me. as I said a couple of minutes of coding has a lot more typing than the saved time, also remembering the aliases has a cognitive cost, that again, to me is not worth it, of course if I where a sysadmin and had to repeat the same commands hundreds of times it would probably be different, or if in your case you never have to deal with machines without bash. Almost everything is a trade-off that has to be evaluated case by case; I just wanted to pointed out the other side of the trade-off. And in that note, DRY is also not a universal law, it also carries it's own trade-offs, to evaluate them and take action in a case by case basis is part of our job.

Collapse
 
dsbarnes profile image
dsbarnes • Edited

glo is what I needed in my life - Thank you.

Getting a little more outlandish with this, I often have a need to do git checkout origin/Whatever

We can do this:

studentBranch() {
    git checkout origin/$1
}

alias sb=studentBranch

Aliasing with functions allows us to pass parameters to our aliases :D

Collapse
 
mojemoron profile image
Micheal Ojemoron

Wow, l like this πŸ‘πŸ»

Collapse
 
akhilrs profile image
Akhil R S • Edited

I think the best approach is to add the alias in .gitconfig instead of adding them in .bashrc or .zshrc, as Dave mentioned.

I am also using legit for the frequent operations, like pull, push, checkout etc.

github.com/frostming/legit

Collapse
 
mojemoron profile image
Micheal Ojemoron

yeah, Uncoupling git aliases from the shell environment is a better way to go, thanks for the feedback.
I will check out your aliases :)

Collapse
 
delaudio profile image
delaudio

I use oh my zsh and its shortcuts:
github.com/ohmyzsh/ohmyzsh/wiki/Ch...
I find them and other aliases available very easy and useful

Collapse
 
notngan profile image
Ngan B. Nguyen

metoo

Collapse
 
mojemoron profile image
Micheal Ojemoron • Edited

I am joining soon :)

Collapse
 
mojemoron profile image
Micheal Ojemoron

Wow, I am loving this. Thanks

 
190245 profile image
Dave

Of course, but I wouldn't put things like this into .bash_profile - .bashrc is preferred (for me). I'm not always in a login shell.

To me, git configuration, like shortcuts & PGP keys (that I shared with the OP) should be in the .gitconfig file, so if I'm looking to change them, that's the first place I'd look (on someone else's system/login).

CLI shortcuts, such as renaming "git" to "g" are correct in .bashrc - and indeed, you could reference git aliases from bash aliases, but at some stage, I think we descend into acronym hell. gpf expanding to git push -f would be OK, but I've seen some bad examples too.

Collapse
 
ahmedghazi profile image
Ahmed

shorthand.css is the same, concise, fast

Collapse
 
mojemoron profile image
Micheal Ojemoron

Beautiful!

Collapse
 
190245 profile image
Dave

It's the OO dev in me - Single Responsibility Principle. ;)

Collapse
 
waylonwalker profile image
Waylon Walker

I love a good set of aliases! I've been usingforgit for quite awhile now. The fuzzy matching is very nice.

Collapse
 
mojemoron profile image
Micheal Ojemoron

Beautiful!