Click here to Skip to main content
15,911,360 members
Articles / All Topics

Setting Up Git in Windows with Diffmerge, Powershell, Posh-Git, and Console2

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
2 Dec 2011CPOL7 min read 16.1K   1  
Setting Up Git in Windows with Diffmerge, Powershell, Posh-Git, and Console2

Isn't that just an admirably long title? I mean, wow. Lots of stuff in there. Today I figured I would post the steps needed to setup your Git environment like mine. It was a challenge for me to do this initially, since no one single source found through the magic of Google was the final solution for the issues I had in achieving this ultimate setup of geektasticness.

The first question most I imagine would have for me is: 'Why?' It's simple; of course you can just install MSysGit and go on your happy way enjoying the semigeekness of doing everything git and such through the pseudo bash command console that comes with the installation. But (at least if you are a windows user), why would you want to use a non Windows native console when there is a perfectly good one built into Windows Vista and Windows 7 called Windows PowerShell? I mean, if you are a windows/.NET developer, you may as well go all out and use the Microsoft tools.

We'll start with the most basic and move up to the most awesome.

Step 1: Installing MSysGit

Go to http://code.google.com/p/msysgit/downloads/list and download the 'full installer' version; be sure that you are not downloading the self contained version. Once downloaded, go ahead and run the install. Once you get to this screen:

Be sure to select the second or third option. It warns you on the third option that it will override a few of the built in windows command line tools, but they're not ones I use anyways, so it's a non-issue for me. Once you have this installed, you can stop if you want, since you now have a fully functional git environment. But you are stuck with using the default tools, such as KDiff (which in my opinion is ugly, and is missing a couple features that Diffmerge offers), and Vi as the editor. I'm not sure about you, but every time I've forgotten to include a '-m' (message attribute) in my commit commands, I pretty much panic, because I can never remember how to exit Vi. One of these days, I'll sit down and spend some time learning Vi, but there are too many other things I'm working on at the moment. (Tip: During the msysgit install, I also suggest choosing the windows line ending option.)

Step 2: Setting up an Alternative editor

The first thing I suggest doing, unless you are actually familiar with Vi is changing your default editor in Git. I choose to use plain ol' vanilla Notepad that comes with Windows. I was using Notepad++ but in the end I decided that I really don't need something so feature rich just for editing comments on my Git commits. (Do you really need syntax highlighting or line numbers for this?) Instead I've opted just for notepad. To set this up, you'll need to edit your .gitconfig file. This is located in c:/users//.gitconfig Pop that opens in the text editor of your choice and adds these lines to the bottom:

XML
<script src="https://gist.github.com/1176170.js?file=corestarter"></script> 

If the .gitconfig already has a 'core' section, just replace/add the 'autocrlf' and 'editor' values. Once you have done this, anytime you commit without the '-m' attribute, Notepad will open. It works out very nicely.

Step 3: Setting up Sourcegear Diffmerge

This was the step that was the toughest for me. This really is the most technically difficult step to accomplish, unless you are an adept bash scripter I guess. But having been raised on Windows systems and GUI interfaces, the concept of Bash scripting is foreign to me. After a couple days of looking at a few different examples/tutorials online, I still couldn't get this to work for me. There isn't a tutorial out there that quite matched what I was needing. Finally I sucked up my pride and asked my boss (who already has diffmerge working on his system) if I could take a look at his setup, and that was the final step. So I am going to save you the trouble I went through and provide examples of the .sh files (bash scripts) and .gitconfig configurations you will need for this to work. Of course, you may need to change a couple paths though.

First, you will need to go here and download the latest Windows installer of SourceGear DiffMerge.

Second, you will need these 2 .sh files. I put them in a folder called 'GitConfigFiles' in my user directory.

HTML
<script src="https://gist.github.com/1176170.js?file=diffmerge-diff.sh"></script> 
<script src="https://gist.github.com/1176170.js?file=diffmerge-merge.sh"></script> 

You may need to modify the paths in these two files, depending on where you installed DiffMerge on your system.

And third, open your .gitconfig file back up and delete the 'merge', 'mergetool', 'diff', and 'difftool' sections and paste in this:

HTML
<script src="https://gist.github.com/1176170.js?file=gitconfigEdit"></script> 

Now anytime you fire up your difftool or mergetool from the git command line, DiffMerge will pop up, instead of KDiff. Yay!

Step 4: Experiencing Git through Windows Powershell

Since we are working on a Windows system and not a Unix based system, let's close the bash console and open up Windows Powershell. It is already installed if you are running Windows Vista or newer. By no means am I an expert on Powershell, but if you would like to learn more, you should start here.

Since you installed git to be included on the system path, you can go ahead and start using git from Powershell as is. But you will quickly realize that something is wrong. You no longer have any 'git' related info on your console telling you what branch you're in, and you no longer have branch name tab completion. This is a huge issue. But have no fear! Someone out there has written a series of Powershell scripts that, once setup, give you not only your branch name in the console and branch name tab completion, but also some basic information about the current state of your branch and git command tab completion. Here's a screenshot of some git commands in action:

Note: Posh-Git will only work with Powershell 2.0 or newer.

You'll notice the '[master +2 ~0 -0 !]' in the screen shot. This is telling me that I am in the 'master' branch, 2 files have been added within the repository, no files have been modified, no files have been deleted, and the '!' tells me that the files added have not yet been added to the repository. After I call 'git add .' the text turns blue and the '!' goes away, telling me new files have been added, but have not yet been committed. It's very handy to have this info there for me.

To get this working, you'll need to navigate to the directory on your system where you would like to store the posh-git files with Powershell and type this command:

'git clone https://github.com/dahlbyk/posh-git.git'

This will pull a full copy of all the most recent posh-git files directly from GitHub.

Before going on to the next step, you will need to set Powershell to allow custom scripts to run. To do this, open Windows Powershell and run this command:

'Set-ExecutionPolicy unrestricted'

Now you can navigate to the Post-Git files and run:

'.install.ps1'

This installs the scripts into Powershell for you. Now you can restart your Powershell console you and will have all the Git goodness I told you about.

Step 5: Console2 FTW!

This last step is totally optional, but is a lot of fun. I use an open source program called Console2 to run my other consoles. This gives you a couple extra options you otherwise would not have; i.e.:

  • Easy copy and paste from the console window
  • Configurable background settings
  • Multiple consoles open through different tabs

To get this setup, you'll first need to download Console2. Then copy the Console2 files to a directory on your system; I put them in a folder called 'Console2' under my program files. This application does not require installation, you can now just run it. Once you have it open, go to its settings and click on the 'tabs' section. In here, you will add a new tab and point it to the powershell executable; which is under:

'C:WindowsSystem32WindowsPowerShellv1.0'

Than move up this new tab in the tab order so it is the default tab when you open Console2. You can now also play with the different settings to your hearts content to change the background and fonts of your console.

Closing

If you have made it this far, you now successfully have Git running with Notepad and SourceGear DiffMerge, through Windows Powershell, which is running through Console2. Isn't that just geektastic? Well, I hope this post was helpful. Feel free to ask questions, post comments/corrections, or just generally make fun of me below. This post was made possible by:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Element Fusion
United States United States
I am a web developer for Element Fusion living in Oklahoma City, OK. I've been developing in .Net web technologies since the summer of 2009. I enjoy working in Asp.Net MVC along with tinkering with other new web technologies.

Comments and Discussions

 
-- There are no messages in this forum --