Leverage Git Config & Autocomplete Git Commands

Git Logo

I’ve already discussed customizing your shell and command prompt. To me, it is equally important to leverage Git configuration and autocomplete Git commands. You should also check out how to show the current Git branch in your Bash prompt.

Git Config

There are a lot of cool things you can do to customize Git just the way you like it. Most of these ideas are personalized versions of the git config customizations found at the Git website.

To create succinct, efficient commands in Git, create aliases for both the shell and for Git. Add the following code snippet to your .profile or .bash_profile:

alias gst='git status'
alias gco='git checkout'
alias gci='git commit'
alias grb='git rebase'
alias gbr='git branch'
alias gpl='git pull'
alias gpu='git push'
alias gad='git add -A'
alias gmt='git mergetool'
alias bdf='git diff'
alias glg='git log --date-order --all --graph --format="%C(green)%h%Creset %C(yellow)%an%Creset %C(blue bold)%ar%Creset %C(red bold)%d%Creset%s"'
alias glg2='git log --date-order --all --graph --name-status --format="%C(green)%h%Creset %C(yellow)%an%Creset %C(blue bold)%ar%Creset %C(red bold)%d%Creset%s"'

Next add the following code to your ~/.gitconfig file:

[alias]
st = status
co = checkout
ci = commit
rb = rebase
br = branch
pl = pull
pu = push
ad = add
mt = mergetool
df = diff
lg = log --graph --name-status --oneline

Now reload your shell and you’re good to go. I’d also recommend you configure the following settings:

  • Color UI. Adds color to commands like git status so you can read the output more easily. It’s as simple as git config --global color.ui true.
  • Code Editor. I like to use vim, but Sublime would be a great alternative. git config --global core.editor vim.
  • Diff & Merge Tool. I downloaded the DiffMerge app for my MacBook Pro and the P4Merge for my Windows box. These tools allow me to compare code or resolve code conflicts when I run into them. This is worth the time it takes to set up before hand. Read how at Customizing Git – Git Configuration: External Merge and Diff Tools.

Below is what my .gitconfig file looks like now (Updated 2012-09-28):

[user]
        name = John Doe
        email = johndoe@doe.com
[alias]
        st = status
        co = checkout
        ci = commit
        rb = rebase
        br = branch
        pl = pull
        pu = push
        ad = add
        mt = mergetool
        lg = log --graph --name-status --oneline
[core]
        editor = vim
        #autocrlf = true
[color]
        ui = true
[merge]
        tool = kdiff3
        ff = true
[mergetool "kdiff3"]
        path = /usr/local/bin/kdiff3
        #path = C:/Program Files (x86)/KDiff3/kdiff3.exe

        guitool = kdiff3
[difftool]
        path = /usr/local/bin/kdiff3
        #path = C:/Program Files (x86)/KDiff3/kdiff3.exe

Autocomplete Git Commands

To add autocomplete for your Git commands, download the git-completion.bash file. The easiest way I know to do it is by using the following curl command in the shell:

curl https://github.com/git/git/raw/master/contrib/completion/git-completion.bash -OL

The -O options tells curl to output a local file with the same name as the remote file. Thus, the name of the file is extracted from the given URL.

The -L option allows curl to redirect if the appropriate location is indicated with a Location: header and a 3XX response code. curl will redo the request using the new location.

Once you get the the git-completion.bash file, find a place to store it permanently. I put mine with the rest of my shell scripts in ~/bin. Then add the following code snippet to your .profile or .bash_profile file:

source ~/git-completion.bash

App of the Day: Sublime Text 2

Sublime Text 2 IconThere are a lot of different text/code editors out there. Some people swear by Coda ($99) or TextMate (~$50). Others prefer TextWrangler because it’s free and still has plenty of power to get the job done. If your a PC person, Notepad++ may be your weapon of choice. This post isn’t meant to be a review or comparison of the different text editors out there, but simply an introduction to another awesome alternative that I think is more unheard of than not.

Last summer a close friend referred me to Sublime Text 2. Before Sublime, I used TextWrangler, and I gotta say, the wrangler is great, but I just didn’t connect with it for some reason. I’ll admit, I didn’t really research all its functionality or install very many plugins, but I’ve heard great things. Regardless, Sublime resonated with me instantly.

Sublime is clean; no buttons all over the place. It’s got this awesome “1000 ft” distance view that can be used to scroll fast through code or find a section of your code through pattern recognition in the line structure. I’ve found it very helpful since I often use it to look through error stack traces, which are very pattern prone and are often thousands of lines long. Sublime has syntax highlighting for just about every language possible (much like other editors). You can control the preferences really easily too, changing just about any functionality. For example if you don’t like the “1000 ft view,” you can turn that off. Did I mention autocomplete? Yup…and while this isn’t language specific, it’s surprisingly smart and very helpful. Not nearly as powerful as autocomplete in Xcode, NetBeans or the like, but still a very useful feature.

These are just a few of the features that came to mind first and that I’ve found very helpful. Here’s a list of a few more (This comes straight from the documentation):

Usage

Customization

Miscellaneous

API

Oh and…Sublime is pretty platform agnostic. It’s available on Mac OS X, Linux and Windows (32- & 64-bit). You can also download “portable” versions for Windows that are self-contained so if you just want to try it out or keep it lite, you don’t have to worry about it installing extra garbage in your registry or system folders.

Sublime isn’t technically “free.” Here’s what the author has to say about that:

Sublime Text 2 may be downloaded and evaluated for free, however a license must be purchased for continued use. There is currently no enforced time limit for the evaluation.

So in other words, you can use it indefinitely without any limitations. I think it asks you to buy a license after every 25 saves or something? I didn’t find it very annoying personally. After I realized I really liked it, I just asked my company to buy me a license for work. The downside is that if you can’t work a deal like that then it costs $59 for one license.

Try it out and tell me what you think. What’s it missing? Would you switch?

*UPDATE* You should really check out Tuts+ for a list of very useful tips and tricks for Sublime Text 2. It will blow your mind if you don’t know about these features already.

Sublime Syntax Spell Checking

Syntax Spell Checking

Sublime Syntax Highlighting Javascript in HTML in PHP

Syntax Highlighting Javascript in HTML in PHP? No problem...

Sublime Multiple Selections

Make Multiple Selections

Sublime Find and Replace with Regex

Find and Replace with Regex

Sublime Edit Side-by-Side

Editing side-by-side