Tim Hordern

I ♥ building amazing products and teams. QA + Product + DevOps + Fast = Awesome.

Using Git Include to Add Color to Your Git History

I saw a neat Git alias for viewing your Git history on Git Immersion. I thought I’d share not only the alias, but how to use the new Git include function so you can add the alias from a source control-hosted version of your .dotfiles!

1
git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short

Which when you run this against the Jekyll repo, it gives us:

1
2
3
4
5
6
7
8
9
* 7d88f72 2012-03-03 | avoiding to call site_payload one time per each post and page. Speed site creation up of a 20%. (HEAD, upstream/master, master) [Luca Grulla]
* 3056953 2012-04-23 | Update History. [Tom Preston-Werner]
*   4533e60 2012-04-23 | Merge branch 'master' of https://github.com/daneharrigan/jekyll into daneharrigan-master [Tom Preston-Werner]
|\
| * 316cc85 2012-02-26 | moved paginate_path to default config [Dane Harrigan]
| * 58d8fef 2011-06-05 | added command-line option --paginate_path [Dane Harrigan]
| * 2b8017d 2011-06-04 | can now set a custom pagination location with pagination_path [Dane Harrigan]
| * b2ab245 2011-06-04 | gave the assertion a failure message [Dane Harrigan]
* | 8a0fbf0 2012-04-23 | Cleanup for RDiscount TOC support. Closes #333. [Tom Preston-Werner]

This is pretty nice as it is. But I thought I’d pimp it up a little bit by adding some of the color tags into the format. You set a color by including the %C option in your format, specifying a color, and %Creset when you want the color to end.

1
%C(color)options-to-color-go-here%Creset

Applied to the original alias, we get this:

1
git log --pretty=format:\"%C(yellow)%h%Creset %ad |%C(blue)%d%Creset %s [%C(green)%an%Creset]\" --graph --date=short

Cool, so we now have a nice shiny Git history command. But it’s pretty ugly having to type this everytime. We could add this to our Bash aliases in our .dotfiles, but under Git 1.7.10 released in April we can now use the includes process to directly add it to our Git config.

First, find the right .gitconfig file that you want to configure. The global .gitconfig file lives in ~/.gitconfig, but you could easily apply this to a specific repo’s .gitconfig file.

Add this to your .gitconfig file:

1
2
[include]
  path = .gitaliasconfig

Add this to your .gitignore file:

1
.githubconfig

My .dotfiles are stored in my personal Dropbox, so I navigate over to my .dotfiles folder and add this to a new .gitaliasconfig file there.

1
2
[alias]
  hist = log --pretty=format:\"%C(yellow)%h%Creset %ad | %C(blue)%d%Creset %s [%C(green)%an%Creset]\" --graph --date=short

Back in your ~folder, create a symbolic link to your new .gitaliasconfig.

Creating a symbolic link to .gitaliasconfig
1
ln -s ~/PathToYourDotfiles/.dotfiles/.gitaliasconfig

And just like that you can run git hist and see your new pretty output, with your new alias stored in your .dotfiles config. If you really wanted to get fancy, you could move your config and ignore file to your .dotfiles store and get it all symbolically linked.

You could also use this to move your GitHub config to a private file, or any other specific git configuration you do.

Comments