# Mark Embling

## Blog

### Git Environment on Windows

It is probably evident from previous posts on my blog that I like Git and working with it from within PowerShell. In the past I have talked about tweaking the PowerShell prompt to add git stats and setting up SSH-agent to work from PowerShell, however I have never actually gone through my entire Git/Windows environment and the steps taken in order to set it up. I thought it was time I did.

### Ingredients

Like most Windows Git users, I use the excellent MSysGit. This provides us with all the Git, SSH and Bash binaries, packaged up in a nice friendly installer. Rather than sticking with the MSysGit-provided Bash shell however, I use PowerShell. If you are using any modern version of Windows this will be installed already. It can also be added for those people who are stuck with (or still prefer) Windows XP (link).

In addition to these basic tools, I use the following:

• Console – excellent terminal application with multiple tabs and customization options
• DiffMerge – pretty decent diff/merge application
• Notepad++ – great text editor with tabs, syntax highlighting and no nonsense

### Installing & Setting Up Git

Installing MSysGit is simply a case of running the installer. However it is well worth remembering to choose one of the two options which allows use of the Git commands from the Windows command line (and by extension, PowerShell), otherwise you will be limited to using Git from inside the Bash shell it installs (making this entire guide a bit pointless). I normally use the last option and have found it has no ill effects for my uses.

The next two steps ask you which SSH client you'd like to use and which style of line endings to use. I always select OpenSSH and recommend doing the same unless there's a particular need not to. I always set line endings to use Windows (since I'm almost always working with Windows-centric projects when I'm working from Windows), but feel free to select whichever setting will work best for you.

First of all, you should tell Git who you are. Fire up a PowerShell instance and run the following commands (substituting your name & email for mine of course).

git config --global user.name "Mark Embling"


Assuming this completed without issues, Git should be ready to use. Feel free to set up or clone your repositories now or later. Git will use the user information you have given it on all your future commits.

Installing Notepad++ is again a case of just running the installer. Git can be made to use Notepad++ for all its editing (commit messages for example) using the following command (substituting the path as appropriate).

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


The various flags tell Notepad++ not to display the tab bar, load plugins and a couple of other appropriate settings. Note the position of the single and the double quotes - I've been caught out by missing them out in the past.

### Setting Up DiffMerge

Sometimes during development it is inevitable that you will encounter conflicts where you have modified a given section of a file and someone else you are working with has made a different change to that same file. Although Git has some pretty good smarts when it comes to merges, sometimes it will be impossible for it to resolve automatically. When this happens, there is nothing worse than having no decent merge tool. Sometimes you just want to see what changes you've made - again, having a decent tool helps immensely here. DiffMerge serves both of these purposes well, and can be tied in to Git so that you can call up a diff or a merge when necessary with a single command.

Once again, installation is a simple installer. Once it is installed, it needs to be hooked up to Git. You will need two small scripts, mine are below (again, change the paths as appropriate). I have placed these in C:\Program Files (x86)\git\etc\wrappers, but you can place then wherever suits you best.

#### git-diff-diffmerge-wrapper.sh:

#!/bin/sh
"C:/Program Files (x86)/SourceGear/DiffMerge/DiffMerge.exe" "$1" "$2"


#### git-merge-diffmerge-wrapper.sh:

#!/bin/sh
"C:/Program Files (x86)/SourceGear/DiffMerge/DiffMerge.exe" "$1" "$2" "$3" --result="$4" --title1="Mine" --title2="Merging to: $4" --title3="Theirs"  Having created these wrapper scripts, they can be hooked up to Git inside your Git config file found at C:\Users\<You>\.gitconfig (or C:\Program Files (x86)\Git\etc\gitconfig to affect all users). Add the following sections to the bottom of the file: [diff] tool = diffmerge [difftool "diffmerge"] cmd = 'C:/Program Files (x86)/Git/etc/wrappers/git-diff-diffmerge-wrapper.sh' "$LOCAL" "$REMOTE" [merge] tool = diffmerge [mergetool "diffmerge"] cmd = 'C:/Program Files (x86)/Git/etc/wrappers/git-merge-diffmerge-wrapper.sh' "$PWD/$LOCAL" "$PWD/$BASE" "$PWD/$REMOTE" "$PWD/$MERGED" trustExitCode = false keepBackup = false  Now that DiffMerge is hooked up to Git, you will be able to use git difftool and git mergetool at the command line to open it up. For example, if you have made changes to a file which you have not yet staged for commit, git difftool (for all changed files – it will open them one after another) or git difftool <filename> (if there's a particular file you want to know about) will allow you to compare your changes with how it previously looked. It can be quite useful to recap what changes you've made prior to a commit. Using the git diff and git merge commands will still fire up the built-in (and quite frankly terrible) tools. ### Setting Up Console Installing Console is simply a case of downloading the binaries and copying them to a convenient location. The program's settings are stored alongside it so it is easily carried from one machine to another if necessary. The default settings for Console have it set up to use Command.exe. I normally set up a new tab (Edit → Settings) which uses PowerShell and set it as the default tab type by moving it to the top of the list. The path to the PowerShell executable is generally something like C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe. Settings like the background colour, tab icon, cursor appearance and more can also be tweaked to your liking. ### PowerShell Extras Whenever I set up a new Windows environment, as well as installing everything I've written about here I always change my PowerShell prompt to give me Git information whenever I'm within a Git repository. As I touched on in the introduction, I have written about it and provide the scripts which can be integrated into your PowerShell profile. In addition to this, I also use another script which integrates SSH agent into the PowerShell environment. This is something else I've written about. Note that this relies on the ability to run the ssh-agent binary, so this needs to be located somewhere in the PATH. Feel free to use these as you wish, I find it makes the whole Git/PowerShell experience feel that bit more efficient and polished. ### More Resources Jeremy Skinner has written a great guide to getting started with Git. It is written focusing on GitHub and the MvcContrib project, but is relevant to Windows Git users generally. It's worth noting he makes a couple of slightly different choices to me regarding his setup, so my best advice would be to pick whichever fits your environment and workflow best. A couple of other resources worth checking out: ### Comments Alexander Groß on said: If you use DiffMerge, your config might sometimes fail.$BASE is not passed by Git if there's no common base file. I've come across such cases, though I don't exactly remember how :)

Using Git on Cygwin, I've solved that problem by providing a wrapper script that invokes DiffMerge. It first checks if the $BASE exists, and provides an empty file if it doesn't. Here's more discussion on the issues: <a href="http://support.sourcegear.com/viewtopic.php?f=33&t=10077">http://support.sourcegear.com/viewtopic.php?f=33&t=10077</a> Mark on said: @Alexander: awesome, thanks for the heads-up on this. I have to admit, this is not a situation I've encountered before. Well worth knowing that this situation can exist though. Thanks. Eric on said: Great post. I've just gotten started with Git and Ruby on Rails. So my question is, after I've set up notepad++ to be the default text editor as you've done here, how do you call notepad? For example, the tutorial I am working through uses the command$ mate README.markdown to edit the README.markdown file, but the tutorial uses TextMate for OSx. What would the equivalent command to launch notepad++ be?

Mark on said:

@Eric: you need it to be available in the PATH environment variable. You can then call it just like any other command. The other alternative (and the one I favour) is to set up a PowerShell alias to call it. On my work machine, I have Notepad2 set up for this (rather than Notepad++) and have the following:<br /><br />

<code>function e {<br />
}</code><br /><br />

I can then use it from the shell like this: <code>e myfile.txt</code><br /></br >

Hope that helps.

Terry on said:

Great post. I've implemented most of what you said, but when ever I do a git commit -m"Comment", Console (and Powershell) both then put up an additional prompt with a simple >>. Any idea what that is and/or how to prevent it? As it stands, I can't figure out how to do a commit with a message from Console. I have to go over to Git Bash to do it.

Terry on said:

Great post. I've implemented most of what you said, but when ever I do a git commit -m"Comment", Console (and Powershell) both then put up an additional prompt with a simple &gt;&gt;. Any idea what that is and/or how to prevent it? As it stands, I can't figure out how to do a commit with a message from Console. I have to go over to Git Bash to do it.

Terry on said:

Shoot, sorry for the double post. Got an 'Error occurred' message and tried massaging comment, but I see both of them went.

Terry on said:

And to continue to spam your comments ;) I figured out my problem. I needed a space after -m (strangely enough, it works without the space in Git Bash, and that was format of original example I saw).

Mark on said:

@Terry: glad you got it sorted. I must admit, I've never seen it done without a space before.

averagetip.net on said:

I'm gone to inform my little brother, that he should also pay a quick visit this website on regular basis to get updated from newest gossip.