Warning: Creating default object from empty value in /home/planetozh/planetozh.com/blog/wp-content/plugins/paged-comments/paged-comments.php on line 31

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/planetozh/planetozh.com/blog/wp-content/plugins/paged-comments/paged-comments.php:31) in /home/planetozh/planetozh.com/blog/wp-content/plugins/yourls-wordpress-to-twitter/plugin.php on line 73

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/planetozh/planetozh.com/blog/wp-content/plugins/paged-comments/paged-comments.php:31) in /home/planetozh/planetozh.com/blog/wp-content/plugins/yourls-wordpress-to-twitter/plugin.php on line 73

Warning: Cannot modify header information - headers already sent by (output started at /home/planetozh/planetozh.com/blog/wp-content/plugins/paged-comments/paged-comments.php:31) in /home/planetozh/planetozh.com/blog/wp-content/plugins/wp-super-cache/wp-cache-phase2.php on line 62

Warning: Cannot modify header information - headers already sent by (output started at /home/planetozh/planetozh.com/blog/wp-content/plugins/paged-comments/paged-comments.php:31) in /home/planetozh/planetozh.com/blog/wp-content/plugins/ozh-who-sees-ads/wp_ozh_whoseesads.php on line 630

Warning: Cannot modify header information - headers already sent by (output started at /home/planetozh/planetozh.com/blog/wp-content/plugins/paged-comments/paged-comments.php:31) in /home/planetozh/planetozh.com/blog/wp-content/plugins/ozh-who-sees-ads/wp_ozh_whoseesads.php on line 631

Warning: Cannot modify header information - headers already sent by (output started at /home/planetozh/planetozh.com/blog/wp-content/plugins/paged-comments/paged-comments.php:31) in /home/planetozh/planetozh.com/blog/wp-content/themes/planetozh/functions.php on line 172

Warning: Cannot modify header information - headers already sent by (output started at /home/planetozh/planetozh.com/blog/wp-content/plugins/paged-comments/paged-comments.php:31) in /home/planetozh/planetozh.com/blog/wp-content/themes/planetozh/functions.php on line 173
Efficient Git Setup on Windows « planetOzh
In: , ,
On: 2012 / 11 / 27 Viewed: 216 times
Shorter URL for this post: http://ozh.in/vl

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:

  1. First, Git is so vast that I doubt any GUI could include every possible command and option without making a nightmarish interface.
  2. 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, aka msysgit. 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:

  • in the Appearance section, uncheck "Snap to desktop edges" (silliest setting *ever*)
  • Behavior section: check "Copy on select"
  • Hotkeys: I like to have Control T to create a new tab from the list we'll define later, and Ctrl + V for a keyboard paste.
  • Mouse: Assing Copy/Clear and Select Text to Left, Paste text to Right, and maybe Menu to the Middle button
  • Tab: define a new tab and make it first in the list with the following settings:
    • Title: Git
    • Icon: C:\Program Files (x86)\Git\etc\git.ico
    • Shell: "C:\Program Files (x86)\Git\bin\sh.exe" --login -i (mind the quotes)
    • Startup Dir: D:\home\planetozh or what works for you

For the lazy here is my whole console.xml setting file (ie default settings plus tweaks described here). Drop this file in Console2's directory.

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

TL;DR: a quick .reg hack will give lasers to your right click, pew pew

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).

  1. Windows Registry Editor Version 5.00
  3. [HKEY_CLASSES_ROOT\Directory\Background\shell\git]
  4. @="* Guit Bash"
  5. "NoWorkingDirectory"=""
  7. [HKEY_CLASSES_ROOT\Directory\Background\shell\git\command]
  8. @="C:\\Program Files (x86)\\Console2\\Console.exe -t \"Git\" -d \"%V\""
  10. [HKEY_CLASSES_ROOT\Directory\Background\shell\git_gui]
  11. "NoWorkingDirectory"=""
  12. @="* Git GUI"
  14. [HKEY_CLASSES_ROOT\Directory\Background\shell\git_gui\command]
  15. @="\"C:\\Program Files (x86)\\Git\\bin\\wish.exe\" \"C:\\Program Files (x86)\\Git\\libexec\\git-core\\git-gui\" \"--working-dir\" \"%V\""
  17. [HKEY_CLASSES_ROOT\Directory\shell\git_gui]
  18. @="* Git &GUI Here"
  20. [HKEY_CLASSES_ROOT\Directory\shell\git_gui\command]
  21. @="\"C:\\Program Files (x86)\\Git\\bin\\wish.exe\" \"C:\\Program Files (x86)\\Git\\libexec\\git-core\\git-gui\" \"--working-dir\" \"%1\""
  23. [HKEY_CLASSES_ROOT\Directory\shell\git_shell]
  24. @="* Git Ba&sh Here"
  26. [HKEY_CLASSES_ROOT\Directory\shell\git_shell\command]
  27. @="C:\\Program Files (x86)\\Console2\\Console.exe -t \"Git\" -d \"%1\""

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.

  1. PS1='\[\033[32m$(pwd | sed -e 's!/d/home!-!')\033[36m $(__git_ps1 "%s")\033[0m\]
  2. $ '

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)

Add the following code to your .bash_profile:

  1. if [[ $(git rev-parse --git-dir 2> /dev/null) && -z "$GIT_MOTD" ]]; then
  2.     echo -e "\e[00;32m- \e[01;33mWelcome $(id -u -n)"
  3.     echo -e "\e[00;32m- \e[01;32mGIT BRANCH \e[00;32m-----------------------------------------------------\e[00m"
  4.     git branch
  5.     echo -e "\e[00;32m- \e[01;32mGIT STATUS \e[00;32m-----------------------------------------------------\e[00m"
  6.     git status
  7.     echo -e "\e[00;32m------------------------------------------------------------------\e[00m"
  8.     GIT_MOTD=1;
  9. fi

Screenshot of smart prompt and dynamic MOTD, pew pew :

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.

First, download Nano, yes there are Windows ports, and drop the files in C:\Program Files\Git\bin. Next, launch your shiny Git console and type the following:

  1. git config --global core.editor nano.exe

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:

  1. git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

Bonus trick 2 : make TortoiseGit remember your passwords

TL;DR: C:\Users\Bob\_netrc

By default TortoiseGit doesn't store any password, as TortoiseSVN does (or I'm too dumb to find where this is stored). There are two ways to circumvent this.

First option: you can generate SSH keys (on Github for instance) and save them in Tortoise, but I've heard that it's not obviously obvious.

I don't know what loops Otto had to go through, but in case you want to investigate that solution, this post on SO has what seems like good pointers.

Second option, what I've done: simply save your login and password in a file named _netrc that you'll create in your User directory (eg %USERPROFILE% eg C:\Users\Bob).

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.

  1. @echo off
  2. SET /P HOST=What is the hostname i.e github.com?
  3. SET /P USERNAME=What is your username?
  4. SET /P PASSWORD=What is your password?
  5. echo machine %HOST% login %USERNAME% password %PASSWORD% > "%USERPROFILE%\_netrc"

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:

  1. alias glog="git log --graph --pretty=format:'%Cred%h%Creset %C(cyan)%an%Creset -%C(blue)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative"

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!

Shorter URL

Want to share or tweet this post? Please use this short URL: http://ozh.in/vl


This entry "Efficient Git Setup on Windows" was posted on 27/11/2012 at 6:00 pm and is tagged with , ,
Watch this discussion : Comments RSS 2.0.

4 Blablas

  1. 1
    Tim MacAleese Canada »
    commented, on 23/Nov/13 at 5:45 pm # :

    Thanks for taking the time to write all this up. I may need this guide for future projects that need a friendly Windows setup to interface with Git.

  2. 2
    Fabio Bruna Netherlands »
    thought, on 29/Nov/13 at 11:19 am # :

    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.

  3. 3
    Andrei Sweden »
    commented, on 23/Dec/14 at 1:35 pm # :


    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.

    So Console 2 hint is priceless! Hats off and thanks for sharing!

  4. 4
    Stan United Kingdom »
    wrote, on 11/Jan/15 at 5:35 am # :

    Really appreciate the write-up! I needed to do some work on Windows and was dreading having to deal with the Windows console.

    There was only one more improvement I found I had to make: proper escape code support. I got it to work by installing ansicon and changing

    1. HKEY_CLASSES_ROOT\Directory\shell\git_shell


    1. C:\ansicon\ansicon.exe C:\Program Files (x86)\Console2\Console.exe "-t "Git" -d "%1""


Leave a Reply

Comment Guidelines or Die

  • HTML: You can use these tags: <a href=""> <em> <i> <b> <strong> <blockquote>
  • Posting code: Post raw code (no <> &lt; etc) within appropriate tags : [php][/php], [css][/css], [html][/html], [js][/js], [sql][/sql], [xml][/xml], or generic [code][code]
  • Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar.
  • Spam: Various spam plugins on patrol. I'll put pins in a Voodoo doll if you spam me.
  • I will mark as Spam test comments, all comments with SEO names (ie "My Cool Online Shop" instead of "Joe") or containing forum-like signatures.

Read more ?