I love Git. It’s an invaluable tool that has made it easier than ever to experiment with and manage code. Unfortunately, Git isn’t the easiest tool in the world to use. It depends on maintaining a complex mental model of data structures to use effectively. Luckily, there are a number of graphical tools that help to visualize this complexity, such as SourceTree and GitX. Personally, I prefer a terminal-based workflow. Thankfully, Jonas Fonseca created Tig, a fast and powerful CLI-based Git GUI.

There are a number of tutorials online for Tig (Atlassian has a very good one), but this article will focus on features that are available as of the 2.0 release. I recommend following this tutorial with Tig installed rather than reading straight through.

Getting started with Tig

If you’re on OS X and using Homebrew, getting Tig is as easy as:

brew update && brew install tig

Tig is primarily a viewer for the state of your repo at various points in time, but it can also enhance your Git workflow by helping you view diffs and commit history, manage branches, and even make edits and commit them by integrating with your text editor. To start it up, cd into a Git repo and run tig.

To quit Tig at any time, hit shift-Q.

If you’re just starting out, press the h key to open the Help View. You can use the arrow keys to navigate, or j and k if you prefer Vim-style navigation. The full manual is online.

Main View

main

Hit m to get into the Main View. You are likely to spend a lot of time here. This mode shows the entire history of your project in an easy-to-navigate waterfall, with branches and merges mapped visually. If you have used Github to navigate the history of a repo, this will feel familiar. The nice thing about Tig is that everything is local, so you aren’t limited by an internet connection. You can hit the / key to start searching for any text within the view, which is really handy when you’re looking for a particular commit message. From the Main View, you can hit t to get into Tree View for the highlighted commit, or d to open Diff View.

Tree View

tree

Git is great for tracking changes, but not so much for exploring the state of a project at various points in history. Tree View, especially paired with the Main View, make exploring your project throughout time a breeze. Tree View lets you explore files and directories in your codebase. You can use the arrow keys to go up and down, and the enter key to open files or descend into a directory. If you open a file for viewing, you can hit q to close the file and return to the tree.

Views in Tig work like stacks, and hitting q will return you to the View that you were in previously. If you entered Tree View from the Main View, hitting q will return you to it.

Diff View

diff

Git commits are just patches on the commit that came before it, all the way back to the initial commit. Diffs show you exactly what a commit did to the codebase. Therefore, Tig’s Diff View is extremely useful. From the Main View, hit d to view the diff for the highlighted commit. Alternatively, you can hit enter to open Diff View in a split. Hitting q in either case will take you back to the Main View.

Refs View

ref

Refs View is the visual branch selector GUI you’ve always wanted for Git. Access it by hitting r. It’s actually a GUI for all of the refs in your Git repo, which includes tags. However, it is primarily useful for checking out branches. Just navigate to the branch you want to check out and hit shift-C to switch to it.

Status View

status

Tig’s Status View is a huge improvement over git status. Hit shift+S to open it. Like git status, this View shows you all of the files that are staged to be committed, as well as unstaged and untracked files. Rather than typing git add file.txt, you can just select a file with the arrow keys and hit u to stage it to be committed. Hitting u on a staged file will unstage it. To see the diff for an uncommitted file, navigate to it and hit enter. If you want to make a quick edit from Tig, highlight a file and press e to open it in your default Git editor.

One feature of the Status View that I find particularly useful is pressing ! on a file to revert any changes I made to it since the last commit. Be careful, this is a destructive action and cannot be undone. When you’re ready to commit your changes, hit shift-C to open up your text editor and write a commit message.

Useful tools shouldn’t be difficult

Git is a powerful tool for making snapshots of your code, but it’s difficult to learn and use. Tig, with its powerful views and keybindings, makes it much more manageable. It has fundamentally changed the way that I use Git, and I am much more efficient because of it. I hope that you’ll give Tig a try and get a lot of use out of it too!




Tagged with: