Alright! You want to get up to speed with a new git repository? You got it. Here are some quick reference notes and tools to use to see what’s been going on.
Caveat: I’m a little rushed writing this so it’s going to be more of a highlights post than a fully detailed tutorial. If I feel like there’s a lot of material that I’m having to skip I’ll make more detailed posts on specific points. Or maybe I’ll write up a PDF or epub for handy reference.
Gary Bernhardt is pretty much the guy here. If you want to really get to know git and learn some amazing tricks for working with a git repo as efficiently as you work with code then you should subscribe to Destroy All Software.
To follow along you’ll need to head to Gary’s dotfiles repo and snag all his cool git scripts and aliases.
For the following examples we’ll be analyzing the twitter/bootstrap repository on github.
Let’s get to it!
git-churn is a script that shows the “churn” (modified files) sorted by increasing frequency.
# show churn for the whole repository timeline % git churn # show churn for specific directories % git churn app lib # show churn for a time range % git churn --since="1 month ago" # show churn for a specific author % git churn --author=USERNAME
Let’s check out our repo and see what the big files are.
Cool, so we can see that they probably generate that zipfile automatically or at least rebuild it frequently.
Interestingly, the docs get some significant attention. It seems that the docs are not an afterthought in this repo!
Let’s see what fat has been up to.
% git churn --firstname.lastname@example.org --since='1 month ago' # (snip a bunch) 5 assets/bootstrap.zip 5 assets/js/bootstrap.js 5 assets/js/bootstrap.min.js 5 docs/assets/css/bootstrap.css 5 docs/assets/js/bootstrap-typeahead.js 6 docs/assets/js/bootstrap-scrollspy.js 6 js/bootstrap-scrollspy.js 8 docs/assets/js/bootstrap.min.js 9 docs/assets/bootstrap.zip 9 docs/assets/js/bootstrap.js 12 Makefile
Cool eh? This is really handy when you are joining a new team and want to see what your coworkers have had in the fire.
So now you’ve got a shortlist of Interesting Files from git churn, time to see what’s been going on with them.
You can pass specific files to
git log and get the history for just those files. Handy!
# show the commits on Makefile since a month ago % git log --since='1 month ago' -- Makefile # show the commits with file diffs since a month ago % git log -p --since='1 month ago' -- Makefile
pretty git log
This is provided in Gary’s .githelpers, but if you want to get a glimpse without some of the niceities you can run this:
git log --graph --pretty="tformat:%C(yellow)%h%Creset %Cgreen(%ar)%Creset %C(bold blue)<%an>%Creset %C(red)%d%Creset %s"
To get output like this:
Gorgeous isn’t it? The text in red after a commiter’s name is the branch. From this listing we can see that master is the most current version of the code and is a few commits ahead of v2.0.3. This is a really handy way to see the state of an entire repo, use it.
git-divergence is another script from Gary. This one shows the incoming/outgoing changes between two commits. Super useful for comparing, say, a feature branch against master (or develop, whatever it was branched from).
Let’s checkout those commits between master (which we’re currently on) and v2.0.3.
# on local master % git div v2.0.3
With this output we see the commit message and each of the files altered with line-count diffs.
From this quick snapshot we’re happy to see that the authors in this repo are making small commits with few changes. Be worried if you see lots of commits that have so many files they scroll off the view and huge line diffs.
The vim-fugitive plugin for vim is chock full of git goodness. Even if you aren’t a vim user you might find its blame view helpful.
In vim, with the plugin, in a file that you are interested in analyzing you just run
:Gblame to get a great view of who’s been working on what.
So we have Mark Otto to blame (credit?) for all the “le comments”. :-)
Sorry for the whirlwind pace here. I wrote this straight through in one lunch break. But hopefully you’ve gotten at least a couple tips or tricks, and I’ve narrowly achieved my deadline of a new post this week. Woot!
Keep an eye on: Metior
Metior is a tool that looks like it could be really interesting someday. Currently it’s just an API around the git log configured with some interesting, but basic, reports. The “Future plans” have some good bullet points so I’ll be keeping an eye on this project.