Creating aliases in your git config & bash profile

My fingers have been getting tired typing out so much git stuff lately, I decided to look into putting some aliases in my git config file.

To open the git config file in the Mac OS terminal, type:

git config --global --edit

or (this one is the one you’ll have to use if you have any errors in your .gitconfig — trying to run the line above will throw an error):

$sudo nano ~/.gitconfig

This opens up an editor. You can use a common list of aliases to save your fingers and your time:

[alias]
co = checkout
ci = commit
st = status
br = branch
hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
type = cat-file -t
dump = cat-file -p

The hist alias is nice. It condenses your log feed to a much more readable format. For each commit it will show the sha, the date, the commit message and the name of the person who made the commit — and will do this using much less space than an ordinary git log. Formerly I was using git log — pretty — oneline to get a concise readout, but it doesn’t include very much information about each commit.

(While you’re here, make sure your user name and e-mail are set up correctly)

[user]
name = Eric Broberg
email = your.email@here.com

If git doesn’t know who you are, when you make a contribution to an organization’s repo on github, you won’t get credit for it on that repo’s statistics page!

Another great one is gitpurge, a fairly complicated line to type and one you definitely need an alias for.

We’re going to put this one in the bash profile, not the gitconfig.

So type:

nano ~/.bash_profile

This opens the Nano editor. Inside it, paste this line:

alias gitpurge=’git branch — merged | grep -v “\*” | grep -v “master” | xargs -n 1 git branch -d’

What this does it delete all branches from your local repository that have already been merged into the currently checked out branch. It is extremely useful when you working on a group project and following a workflow in which you cut a new feature branch for every incremental feature, and cease to use that branch as soon as it has been pulled into the organization’s (upstream) master.

A single golf clap? Or a long standing ovation?

By clapping more or less, you can signal to us which stories really stand out.