Jan 22, 2013

All in one Command Prompt for Windows Developers

After using Subversion for many years, with the friendly TortoiseSVN and its powerful user interface, I had a hard time understanding what people saw in Git. Besides the really odd set of commands such as fetch, pull, push, etc… I couldn’t find any decent graphical Git client that would make me warm and cozy.

So after managing to put Space Dinos on GitHub because it seemed like that’s where all the cool kids put their code, I stayed clear from using Git for a long while. I guess I’m not the only one in this situation; the folks over at GitHub heard the cries of pain and they ended up releasing a Windows client which hid all of the complexity under a slick UI. This turned out to be a great way to smoothly ease into Git instead of hitting it head on.

Fast forward to today, I use a Linux workstation every day at work and type all kinds of Git commands in a shell window like I know what I’m doing. I’m by no means an expert but I’ve grown to enjoy using it and now prefer it over SVN. I also surprisingly have been quite enjoying having shell windows opened and typing commands. So much so that I’ve been spending some time trying to reproduce a similarly efficient experience on Windows.

After a fair amount of searching and tweaking, I’ve come across some tools and settings which together have helped my productivity both with Git and the Windows command line. Hopefully someone else will find this helpful and maybe even improve on it.

Command line replacement

It’s no secret that the standard Windows command prompt is very bare. It’s not even possible to resize its window horizontally without digging through the settings. And PowerShell hasn’t improved on this. As a result, many alternatives have appeared over the years to fill the gap.

My favorite right now is called ConEmu. Besides addressing the basic issues such as resizing and copy and paste, it is highly configurable. I’ll leave you to dive into all of its settings as it could take hours to cover them all. Instead let’s look at how we can configure it to start with PowerShell in administrator mode:

  1. Open the settings by clicking on the systen icon enter image description here

  2. Go to Startup/Tasks and add a new task, I called mine PowerShell. Put the following in the Commands box:

    *C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

    Note that the path on your system may vary so you may need to have to look for PowerShell, but if you’re reading this you probably know how to do that. Also note the * at the beginning of the line, this is what tells ConEmu to run the command as an administrator.

    enter image description here

  3. Go to Startup and select PowerShell as your startup task:

    enter image description here

Now, whenever you start ConEmu, you will be running PowerShell as administrator. Make sure you go through the settings and configure everything to your liking.

Visual Studio command prompt

When I work on Blade I like to keep a console opened so I can run builds from the command line. But the Visual Studio Command Prompt shortcut doesn’t use PowerShell. Thankfully, someone already had the answer for us over at StackOverflow.

The script refers to Visual Studio 2010 but it should be trivial to adapt it to 2012 as well.

The easiest way to configure this is to create a folder under your Documents called WindowsPowerShell and in it, create a text file called Microsoft.PowerShell_profile.ps1 then open it and paste the script.

Starting ConEmu should now present you with a PowerShell session configured with the Visual Studio environment variables. If you get an error message, it’s likely that you will need to enable script execution. Run the command below and restart ConEmu:

Set-ExecutionPolicy RemoteSigned

Git for Windows

Installing Git seems more complicated than it really is, mostly because the installation settings can be confusing at first. First, get the latest version from the download page and run the installer. You can leave all of the settings to their default except for the PATH environment:

enter image description here

Then let’s install posh-git which will provide some really useful functionality to the PowerShell prompt. First open a new ConEmu window so the Git PATH variable is setup, go to a folder of your choice and run the following commands:

git clone git://github.com/dahlbyk/posh-git.git
cd posh-git
.\install.ps1

At this point, you’re set and you can use git with any remote repository.

SSH keys

A very annoying aspect of using Git is that it will keep asking you for your password all the time. There are several ways to address this but my favorite is to use a SSH key to identify my computer when using SSH to connect to remote repositories.

Git comes with a small tool, the SSH agent, which runs in the background and helps authenticate your computer with remote SSH servers. posh-git ensures the agent is running so all we have to do is create a key and make sure it gets loaded in the agent. Run the following commands:

cd ~\.ssh
& 'C:\Program Files (x86)\Git\bin\ssh-keygen.exe' -f test_rsa

You will be asked for a password, if you don’t want to set any just press enter.

If the first line returns an error, check the troubleshooting section below and make sure the .ssh folder exists under your local user profile folder.

The second line assumes you’ve installed Git in the default location on a 64bits machine, adjust the path accordingly.

You will need to copy and paste the content of the public key (here test_rsa.pub) into the appropriate location on the server. I know both GitHub and BitBucket make it easy to do that in the user profile, I’m guessing other Git hosts do as well.

Finally let’s add a little bit more code to our profile script in order to automatically load all keys into the agent. This will be useful if you use several Git hosts:

$keys = Get-Item "$env:UserProfile\.ssh\*_rsa"
foreach($key in $keys)
{
  Add-SshKey $key.FullName
}

Make sure you add these lines at the end of the file, after posh-git has been loaded.

Troubleshooting

If the tilde ‘~’ character does not work as a shortcut for your home folder, add the following line to your profile script:

(get-psprovider FileSystem).Home = (resolve-path $env:UserProfile)

If your home folder is different from your local profile folder, the SSH agent may not be able to keep track of the known remote servers and it will ask you to accept their certificate every time. Adding the following line to your profile script will fix that:

$env:HOME = $env:UserProfile
Comments powered by Disqus