Friday, February 7, 2014

Customizing Git

By:
In the beginning, I used to use Git exclusively via the command line interface (CLI).  Later on, I've gotten used to using various Git GUI tools for OS X such as Gitbox and SourceTree.  However, with the latest Mavericks release, I've been seeing a lot of instability with these tools so I've been going back to the command line for a while and I've discovered a few helpful tips that can make your life easier.

The first thing you'll want to do is make sure your ~/.gitconfig file is up to snuff.  This file holds various Git preferences such as the username and email that is used for every commit.  For details on how you can customize this file, see Customizing Git Configuration.  For instance, I've made sure that Araxis Merge is the diff and merge tool used by Git:


[diff]
        tool = araxis
[difftool]
        prompt = false
[merge]
        tool = araxis
[mergetool]
        trustExitCode = true


A few Git aliases I like to use include:


[alias]
        ci = commit
        co = checkout
        st = status
        branches = branch
        lol = log --graph --decorate --pretty=oneline --abbrev-commit
        lola = log --graph --decorate --pretty=oneline --abbrev-commit --all
        ls = ls-files
        gblame = gui blame


Next you will want to add a Git completion script appropriate for your shell environment.  This script can help you auto-complete branch paths and more.  Simply create a ~/.git-completion.sh file at your root directory and paste in the contents of the script appropriate for your shell environment and add it to your shell startup configuration (~/.bash_profile in my case) like so:


source ~/.git-completion.sh


Finally, you can show what branch you're currently working on via a Git prompt script.  Like the Git completion script, you'll want to create a ~/.git-prompt.sh file at your root directory and then add it to your shell startup configuration similar to the git-completion script.  For example, when working on the master branch of the Librarian project, my shell prompt looks like:


[abraginsky@darthomir Librarian (master)]$


Obviously, you can configure Git in many more ways than this.  Nothing beats the main documentation and a book I recommend reading is Version Control with Git.

Hopefully you've found this helpful!  Feel free to post any other Git command line tips and tricks that you like to use.

Thanks to @jarjar2k7 for some of these tips.

1 comments:

Unknown said...

Thanks for the tips. I've been using SourceTree on Mavericks without too many issues - i think it did crash on me once now that I think of it.

I started digging into my copy of oh-my-zsh and found a bunch of the things you've referenced built in there. I'm sure there are some differences in implementation, and I've not used the CLI very much for git. Only had to do CLI when something runs afoul in SourceTree - in which case, I'm sure the power of these CLI customizations shine through.

The git* oh-my-zsh plugins:
https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins