04 January 2015

Historically, the story was that Git was great, but not if you wanted to use it on Windows.  Windows was a second-class citizen and you could make it work, but running into trouble was inevitable and then when you did and sought help, all you would get from the Linux-ites that know something was ridicule.  Answers were hard to find and life was Hell.  That is no longer the case and hasn’t been for quite a while.  Using Git on Windows is now a breeze and there’s only one potential good reason you should ever avoid it (that reason is that you prefer Mercurial – a place I used to live, but have now come to the conclusion that one is not universally better than the other and that they both shine in their own ways and have their own annoying quirks).  I intend, here, to show some of how to use Git on Windows in a way that is pleasant and pleasurable.

image

First – installation requires so little effort as to be trivial to the point of not needing mention.  This is the best time in human history to be alive because everything in the world is getting easier with every passing moment (the best time up until now, tomorrow will be even better).  With Chocolatey – a package manager for Windows – installing, uninstalling, and updating software is a snap and can be done from a PowerShell command line with very little user interaction required.  This enables easy scripting of everything a developer requires to be off and running with all the bell and whistles needed for remarkable productivity.  Rob Reynolds, the creator and head maintainer of Chocolatey, calls it apt-get for Windows.  If you are familiar with using apt-get or yum on Linux or HomeBrew on OS X, or installing packages with Nuget or Node Package Manager or Gems with Ruby Gems, you know what Chocolatey is.  Like Gems and NPM and Nuget make installing dependencies into your software project really easy, Chocolatey makes installing software into Windows painless.  Really, Chocolatey is great and if you are using Windows, you should be using it.  Installing Git onto a Windows machine with Chocolatey already installed is then as simple from a PowerShell prompt as:

choco install git

There, wasn’t that easy?  You should also know that GitHub has a Windows client that is pretty cool too.  I have it in my dev machine setup scripts, but I rarely ever bother to launch it.  With the command line interface of Git doing everything I want and the GitHub website having an extensively great keyboard story, there’s just not much reason I find myself wanting anything GitHub for Windows has to offer.  It’s a pretty good example, though, of a WPF application with a Metro-ish design (or whatever the approved name is now) deployed via ClickOnce.  That’s interesting and awesome and the incomparable Phil Haack was largely responsible for its creation, which is also cool (and his employment at GitHub is another example of how far Windows has come in the Git-loving world), but I still don’t have a lot of use for it.  It can also be installed via Chocolatey with:

choco install githubforwindows

Thus, installation of Git was easy and it’s something that should just happen automatically anytime you set up a new machine you’ll use for development.  The best way to make this happen is by creating a script to install the things you want/like/need on your machine and have BoxStarter use Chocolatey to install it all for you.  Mine is here.

With the client installed, there are still a few things you’ll want to do to make everything great and happy as a Windows developer using Git.  One of them is not very straightforward, so I’ll do my best to make it easy here.

git difftool

When committing to source control, or just trying to understand what you have changed in order to proceed with implementing some feature or switching context to deal with an emergency, it is important to be able to see what is dirty in your working directory.  I love the command line and issuing a git diff is fine for a lot of purposes, but when I really want to dig into seeing what I have done, I really want to use a visual diff software tool.  My preference for this is to use Winmerge (don’t download from there – install using Chocolatey) on Windows and FileMerge when using OS X.  I know people who swear BeyondCompare is much better and worth the (small) price.  I’ve yet to determine what is better and so I continue to use Winmerge, though Beyond Compare’s cross-platform availability is a pretty strong case.  It might be worth another look at some point.  Confguring git to use Winmerge is not as easy as it should be.  There are many answers to the question of how to set it up on Stack Overflow, and most of them don’t work without some tweaks.  What I finally landed on is having this in my .gitconfig:

[diff]
    tool = winmerge
[difftool]
    prompt = false
[difftool "winmerge"]
    cmd = winmergeu.exe -e -u-x -wl -wr -dl base -dr mine \"$LOCAL\" \"$REMOTE\" -wl -wr -dl base -dr mine \"$LOCAL\" \"$REMOTE\"

Hopefully that will make it easier for you than hunting through the disparate information and the effort I had to undertake the first time I tried to get this working.

With this configuration in place, simply use:

git difftool

to have git sequentially pop up a Winmerge diff showing the changes you have made but not yet staged, file by file.  You can also use:

git difftool --cached

to have git sequentially pop up a Winmerge diff showing the changes you have made and staged, file by file.

Posh-Git

Posh-Git is a must-use PowerShell module/set of scripts that makes life joyous and wondrous.  The easy way to install it is via Chocolatey.  There is also a port of it for Mercurial, called Posh-Hg.  It’s equally awesome and I only mention it here because if you want to use both, they don’t play nicely together and just installing them via Chocolatey doesn’t work.  There is a Chocolatey package that is supposed to make them both work, but it didn’t work when last I tried (which was a while ago, might be worth giving it a shot now) and I just wound up cloning both repositories and setting up my Powershell profile to use them both.  Anyway, Posh-Git turns your PowerShell prompt into a dashboard showing git status information, making it really easy to see what is happening without having to key git status.  It also, perhaps even more valuably, expands on Powershell’s tab completion so that things like git add become sensitive to which files you have dirty in your working directory and things like that.  It’s really powerful and you must use it.

image

Credentials for Remotes

If you want to avoid keying your GitHub (or BitBucket or whatever) user name and password every time you try to pull/push to/from a remote repository (and you do), you have a few options.  The easiest way is to go ahead and use https for your remotes and install a package to manage https credentials.  Another way is to stop using https and use SSH instead.  If you are in an environment with ports reaching the outside world limited and you are only able to use http/https, the former is a better bet.  If not, you can use either.  I feel a little better about credentials management using SSH because I have one less piece of software I am trusting with my credentials that could have security holes, but it’s probably not a problem either way.



blog comments powered by Disqus