planetOzh, Efficient Git Setup on Windows, Published

I think I've set up a pretty efficient Git environment on Windows and, as promised in a previous post about my Git initiatic journey, I'm sharing it, hoping it'll be useful to someone. Another quite lengthy post I'm afraid, feel free to grab a cup of something before you start reading.

Foreword on client setup: GUI if you want, but command line anyway

TL;DR: GUI guy or not, you'll want to install a shell client.

Most guides about Git state that Git clients on Windows are less polished than SVN ones: I believe that's an outdated statement. If you're used to TortoiseSVN, you'll quickly feel home with TortoiseGit.

This said, I also believe that you should not overlook making yourself comfortable with the command line shell, for 2 reasons:

First, Git is so vast that I doubt any GUI could include every possible command and option without making a nightmarish interface.

Second and above all, every resources and tutorial you'll find will describe command line operations, so it's actually easier to learn how things work in the console, and eventually find what's feasible and easier with the GUI

A quick personal note about the Github client: I find it very limited. It's sexy, it's appealing, but its most useful feature is the "Open command line shell here" button. Do yourself a favor, save some time and skip it.

Installing msysgit then TortoiseGit

TL;DR: Default options FTW, except "Cheetah"

First, you have to install Git for Windows, akamsysgit. I don't have much to say here, just click "Next" when asked for some option, except on that one screen: when prompted for Windows Explorer integration, be sure to select and check "Context menu entries" and not the "cheetah" extension.

The reason is simple: those context menu entries are settings in the registry, and we'll hack and enhance them a lot later.

Next, install TortoiseGit, which is the GUI wrapper around Git. I don't remember doing anything that wasn't default during the setup so I won't elaborate further.

In the rest of this guide I'll assume all install paths are default. You may have to adjust accordingly to your config, in particular regarding the location of the "Program Files" folder which differs from 32 to 64 bits setups.

One thing to be aware of, if you are already a TortoiseSVN user: both "Tortoise" will share the same icon overlay. Unfortunately, it's impossible to visually differentiate the type of repository ; that would have been very neat but it's a Windows limitation and won't change soon.

Shell enhancement: Console2 to the rescue

TL;DR: cmd.exe sucks. But cmd + Console2 with tweaked settings rocks.

Think what you will about Microsoft, but there's one thing they've mastered to an unmatched level: sticking with the same patheticly crappy command shell (aka cmd.exe) since 1914. Recentish versions of Windows ship with Powershell, but that's not better.

Installing msysgit and the Windows port of many Linux utilies will provide several enhancements to the command line shell already (tab completion on commands, aliases for example) but we want more.

Console2 is shell wrapper (not a replacement to the good ol' cmd.exe) that adds what's missing: mouse support to select, copy and paste, a tabbed interface, customizable shortcuts and more. You'll love it.

Console2 is pretty cool but, probably because it didn't want to ashame cmd.exe too hard, it comes with really stupid default settings that need urgent tweaking. The following will make Console2 behave the same as your favorite Putty shell console:

The original Console2 project hasn't been updated for month, but the package you'll get from Sourceforge works fine on Win7. This said, I've found that someone has forked the project and provides a newer version with a few additions.

Regedit wizardry: ultimate context menus

Now you have a fine tuned Console2, let's improve the right click context menus msysgit has installed. The default context menus pop up when you right click a folder icon and start Git's bash in cmd.exe, but with the help of Regedit we'll make that waaaaay better:

context menus will also appear when you click inside a folder (in the white background, not on any icon)

they will launch the improved Console2 shell instead of the cwappy cmd.exe

First, dig in HKEY_CLASSES_ROOT\Directory\shell\git_shell. The trick is to replace the default Git bash command with a shortcut to the Git tab in Console2: C:\Program Files (x86)\Console2\Console.exe -t "Git" -d "%1"

The second trick is to create a Regedit entry to take care of your right-clicking in the background area of a folder: create a new key named git in HKEY_CLASSES_ROOT\Directory\Background\shell with an empty string named NoWorkingDirectory and, in the Console2 command line, replace the directory argument %1 with %V (go figure).

Copy that into a raw text file named blah.reg on your desktop, edit if needed (make sure the paths match, mine here are defaults (keep the double backslash mayhem intact)) and execute the reg file to merge into your registry.

Spicing it all: .bash_profile hints

TL;DR: smart prompt and dynamic MOTD for the win.

You're almost there! Now that you have a cool shell that'll work just the same way as when you're using Putty or a Unix shell, let's leverage what bash has to offer.

First, a nicer and smarter prompt. Mine shows current path and current branch if applicable. Also, since I don't work in what Windows considers my home directory (ie C:\Users\Ozh) but in D:\home\planetozh, the $(pwd | sed) trick here shortens the displayed path to saves a few chars.

Add this to C:\Users\Bobby\.bash_profile (hint: how to create dot files in Windows if you're having trouble with this)

Next, a useful dynamic MOTD.

When you launch a new shell, you're shown the content of the default MOTD (located in C:\Program Files\Git\etc\motd) which just contains a completely useless reminder about git --help.

Upon launch, my shell checks if we're in a git project (ie if git rev-parse --git-dir triggers an error or returns something) and, if so, calls git branch to list existing branches and git status, which is a really handy command that suggests hints on what to do next as well as a few quick sanity checks and reminders (untracked files you've forgotten to git add for instance)

Bonus trick 1 : get rid of vi

TL;DR: not a Linux bearded geek, right? Hate vi, right?

I'm going to lose a lot of internet geek points here but I'll say it anyway: I've always considered Vi (or Vim), which is the default editor on many setups, as the most efficient user repellent ever. Whenever I log on a new shell, the first thing I check is if nano or pico are available (for the clueless: intuitive and simple text editors where you don't need insane key combo such as !:qomg to execute a simple command). The default console text editor in msysgit is, guess what, vi, so we'll simplify things ASAP.

Now, interactive commands such as git commit will send you into nano. I thought it made sense to keep things in the console when you work from the console, but if you want to open your regular text editor instead, say, Notepad++, that's not a problem:

The syntax is simply: machine blah.com login bob password zomgp4ssw0rd with one line per machine

If you frequently work with multiple repositories and different logins, here is a simple .bat file that prompts you for the needed info (host, login, password) and stores everything in the adequate file.

Paste that into gitpass.bat in your htdocs root and just run it everytime you need to store a new login/password pair.

Further improvements

TL;DR: fine, don't read! I won't make it any longer!

You'll probably want to dwelve in crafting your own Git aliases for enhanced command line productivity. There are plenty of tutorials on this everywhere and I'm not personally a fervent user of aliases, which I find to be quite blockers once you find yourself on another computer or shell that does not have them. The only one I have right now, and don't use except to show off when my wife enters the vicinity is:

The next thing I want to play with is git hooks, which are simple text files (hint: everything in Git is simple text files, seriously, SVN, steal that please) to perform more or less complex automated tasks, such as having a branch sync with another one. I'm sure there are lots of crazy fun stuff to do with hooks :)

If you have any other improvement that makes Git any easier or faster, be sure to share!

Thanks, some great tips here. And indeed, Console2 is fine. Keeps me wondering why there hasn't been any improvement on the Windows shell/CMD/DOS. Not that I can see anyhow. If you're a Powershell developer that must be a bit annoying.

I am just starting out with Git on Windows. I have strong Linux background (I use it at home day-to-day for about 10 years), I really-really-really HATE Windows cmd.exe . I miss KDE's konsole and yakuake.

I love WordPress plugins so I wrote a book!

This site

This is planetOzh, weblog of Ozh, a 35 years old dude living in France with an interest for, errr, like, computer things. I have a wife, 2 kids, and one computer-unrelated job. Give my eye a gentle click to read more about me.