Mastering git log for Collaboration

January 16, 2015

Git is an incredibly powerful platform for collaboration. The ability to create light-weight branches as well as push to and pull from multiple remote repos is the foundation of this power. Learning to harness the git log command will help with visualizing all the information that supports these features, refining and invigorating your current workflows.

Learn Git Branching

Obviously, to take full advantage of the collaborative features of Git, you first have to learn how to use Git branching. There’s an excellent interactive tutorial that instructs by converting the Git commands you run into fancy web animations. If you have a basic understanding of Git but have always wanted to learn about some of its more powerful features, this is the best tutorial out there. For a taste of how it works, there’s also a non-interactive demo that features the animations which make this tutorial so well-designed.

After I ran through this tutorial, I felt for the first time like I really knew Git. Unfortunately, being a visual learner, I struggled when trying to apply the same abstractions to actual git repositories. Shortly thereafter, I discovered that the git log command actually has features to replicate many of the same visualizations, but from right within the terminal!

git log

The following is a set of shell aliases that let you run git log with a lengthy set of flags culminating in some pretty awesome Git logs. I’m going to leave a discussion of what the individual flags do to the man pages and just skip straight to the good part. Feel free to copy and paste these into your ~/.bashrc, ~/.zshrc, or similar file.

pretty git log aliases
# pretty Git log
alias gl='git log --graph --pretty="%C(bold green)%h%Creset%C(auto)%d%Creset %s"'

# pretty Git log, all references
alias gll='gl --all'

# pretty Git log, show authors
alias gla='git log --graph --pretty="%C(bold green)%h%Creset %C(yellow)%an%Creset%C(auto)%d%Creset %s"'

# pretty Git log, all references, show authors
alias glla='gla --all'

As you can see, there are four aliases, and each does something a little different. The first, gl, just shows the graph for the current branch. gll shows the graph for all branches, including those which may have diverged from the current branch. Then there’s a variation on each of these, gla and glla, which add author information to the logs produced by their companion.

On top of it all, I’ve customized the colors to work especially nicely if you’re using the Solarized color scheme in your terminal. If you don’t do anything but copy the above aliases, the only thing that will look different from the screenshots below is the text between the parentheses, which will instead all be the same color. To color these the same as below, add this text to the end of your ~/.gitconfig file:

Global Git configuration settings
[color "decorate"]
  head = bold white
  branch = bold magenta
  remotebranch = blue
  tag = bold red

Of course, feel free to tinker with these colors to your liking.

Screenshots

Here are an abundant number of screenshots showing what the commands look like for the TartanHacks website and the Autolab Project.

gl

Even when just using gl, you’ll be able to see all branches that lie further down on the tree. For example, from the add_gitignore branch we can see the origin/gh-pages branch because it’s along the same path in history.

gll

Here we see that multiple branches have diverged from origin/gh-pages; both add_gitignore and fix_seo_and_readability share origin/gh-pages as a common ancestor, but neither have anything in common with the other, which is why fix_seo_and_readability only showed up once we used gll.

gla

Same as gl above, but with author information!

glla

Same as gll above, but again with author information!


Here are the same four examples, but for the Autolab repo. It’s a little more involved because more people are working on it simultaneously. For projects like this, which have several open pull requests and feature branches, these aliases really shine.

gl

gll

gla

glla

Dotfiles

If you’re hungry for more handy Git aliases or just some general ways to beef up your terminal experience, you can find these four aliases and more in the zshrc in my dotfiles repository on GitHub.

Update

After testing out these aliases on various environments, I discovered that one of the features I was using in the pretty format (the one that colors remotes and branches, %C(auto)) is not available in older versions of Git. These are the revised versions of the above aliases that I use on older machines:

Compatible Git log aliases
# pretty Git log
alias gl='git log --graph --pretty="%C(bold green)%h%Creset%C(blue)%d%Creset %s"'

# pretty Git log, all references
alias gll='gl --all'

# pretty Git log, show authors
alias gla='git log --graph --pretty="%C(bold green)%h%Creset %C(yellow)%an%Creset%C(blue)%d%Creset %s"'

# pretty Git log, all references, show authors
alias glla='gla --all'

The only real difference is that all the remotes, branches, and tags are blue, instead of being configurable in you ~/.gitconfig file.


Jake on the Web

If you cared enough to read that far, you should consider following me on GitHub or paying a visit to my homepage. If this post was about one of my open source projects, make sure to star it on GitHub! I love hearing what people think, so feel free to open an issue or send me an email.

Haskell Development with Neovim

After a year and a half of using Haskell on and off, I’ve finally settled on a set of high-quality development and editor tools, using Stack and Neovim. Continue reading

Reach for Markdown, not LaTeX

Published on February 26, 2017

Troubleshooting Haskell Stack Setup on OS X

Published on August 03, 2016