A few handy Git Tips

Because I use git on a daily basis, I’ve found that over the course of several years, I’ve cobbled together a pretty unique group of settings.

I’d like to highlight some of the interesting git configuration tricks that I’ve found over the years.

Autocorrect Typos

As everyone does, I frequently fat-finger a few git commands.

Git has a neat feature built in that it will autocorrect your typos!

To set it up, run:

git config --global help.autocorrect 10

This will tell Git to wait just a brief period of time before running the ‘best guess’ for your command. The small period of time is intended to allow you some reaction time to cancel it, just in case.

Whitespace Warnings

As many a developer will attest, the only thing worse than a file full of mixed tabs and spaces is a file full of trailing whitespace.

Git has a really handy feature to let you know if you’re about to commit the unforgivable act of committing whitespace.

git config --global apply.whitespace warn

Running this command will assure that any of your repositories will tell you if you have trailing whitespace, so that you could fix it before sharing it with the world.

Beautiful Git Log

One of the frequent steps in my workflow is to inspect the git log to see how the project is progressing, as well as identify any possible issues with merging branches.

If you just try to run git log by itself, you get a LOT of information. Sometimes it’s too much.

Because of this, I have two git log commands that I frequently use. There is one for projects that I maintain by myself, and one for ‘shared’ projects; and I’ll explain the difference in just a moment.

The first git log command is actually an alias for the command:

git log --graph --decorate --pretty=oneline --abbrev-commit

If you run this command, you’ll notice that it cuts out much of the info, and only shows the ‘necessary’ info.

The second is almost exactly the same as the first, but with a few tidbits of extra info.

git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

Each line of the log graph will now include a date of the commit, relative to today, as well as the name of the person who made the commit. This makes it super easy to scan through a long log to see who did what and when.

Obviously, you don’t want to type this all the time, so you can run the command:

git alias lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

Then you simply type git lg to get the condensed information.

Auto Rebase

Another cool trick is to use auto-rebase.

Normally, if you have a branch with some commits that aren’t pushed, and someone else also commits and pushes on that branch, when you pull, git will create a commit merging your commits to the commits upstream.

I think that sentence makes sense.

Anyway, because this ‘merge’ commit to rebase your recent changes on top of theirs is meaningless, I prefer to set up ‘auto rebase on pull’ through the config setting:

git config --global branch.autosetuprebase always

With this setting, git will try to reapply your commits with the current version of the upstream branches every time you pull.

Posted on May 10, 2015