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, 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).
- Windows Registry Editor Version 5.00
- @="* Guit Bash"
- @="C:\\Program Files (x86)\\Console2\\Console.exe -t \"Git\" -d \"%V\""
- @="* Git GUI"
- @="\"C:\\Program Files (x86)\\Git\\bin\\wish.exe\" \"C:\\Program Files (x86)\\Git\\libexec\\git-core\\git-gui\" \"--working-dir\" \"%V\""
- @="* Git &GUI Here"
- @="\"C:\\Program Files (x86)\\Git\\bin\\wish.exe\" \"C:\\Program Files (x86)\\Git\\libexec\\git-core\\git-gui\" \"--working-dir\" \"%1\""
- @="* Git Ba&sh Here"
- @="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.
- PS1='\[\033[32m$(pwd | sed -e 's!/d/home!-!')\033[36m $(__git_ps1 "%s")\033[0m\]
- $ '
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:
- if [[ $(git rev-parse --git-dir 2> /dev/null) && -z "$GIT_MOTD" ]]; then
- echo -e "\e[00;32m- \e[01;33mWelcome $(id -u -n)"
- echo -e "\e[00;32m- \e[01;32mGIT BRANCH \e[00;32m-----------------------------------------------------\e[00m"
- git branch
- echo -e "\e[00;32m- \e[01;32mGIT STATUS \e[00;32m-----------------------------------------------------\e[00m"
- git status
- echo -e "\e[00;32m------------------------------------------------------------------\e[00m"
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:
- 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:
- git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
Bonus trick 2 : make TortoiseGit remember your passwords
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.
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.
- @echo off
- SET /P HOST=What is the hostname i.e github.com?
- SET /P USERNAME=What is your username?
- SET /P PASSWORD=What is your password?
- 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.
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:
- 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!
Want to share or tweet this post? Please use this short URL: http://ozh.in/vl