How to configure Git on Windows

In the previous article, I’ve shown how to install Git on Windows 7, but before you can start using it, there’s some basic configuration that needs to be done.

In this article I’ll show how to setup your user name and email address, and configuring external tools like Notepad++ and Beyond Compare to edit your comments and to visualize changes in your code.

Before proceeding, open Cygwin bash shell (Start –> All Programs -> Cygwin -> Cygwin Bash Shell).

Setting user name and email

Git needs to know your user name and email address to properly credit your commits,

$ git config --global user.name "Your Name"
$ git config --global user.email your@email.com

NOTE: The “email” setting doesn’t need to a valid email address, it only needs to match the “user@server naming scheme.

this will setup your user name and email address globally within Git. If you want, you can override this settings on a per-repository basis:

$ cd your_repository
$ git config user.name "Your Name"
$ git config user.email your@email.com

 

Setting the core editor

To properly use Notepad++ editor, you need to create a wrapper script “git-core-editor.sh” with the content,

#!/bin/sh
"C:/Program Files (x86)/Notepad++/notepad++.exe" -multiInst -notabbar -nosession $*

and save it at the path “C:\Cygwin\usr\local\bin”.

Now, lets proceed with the editor configuration:

$ git config --global core.editor /usr/local/bin/git-core-editor.sh

The next time you commit something,

$ git commit

Notepad++ will show up, type your comments, save and close.

Setting the diff tool

Similar to the editor configuration, you also need to create a wrapper script “git-difftool-warpper.sh” with the content,

#!/bin/sh
REMOTE="$(cygpath -w "${1}")";
LOCAL=${2};
echo Launching BComp.exe $REMOTE $LOCAL
"C:/Program Files (x86)/Beyond Compare 3/BComp.exe" $REMOTE $LOCAL

and save it at the path “C:\Cygwin\usr\local\bin”.

Now, lets proceed with diff tool configuration:

$ git config --global diff.tool bc3
$ git config --global difftool.bc3.cmd "git-difftool-wrapper.sh \"\$LOCAL\" \"\$REMOTE\""
$ git config --global difftool.prompt false

To visualize your code changes

$ git difftool <file_name>

That’s all you need to start using Git.

Shout it

How to install Git on Windows

If you want to install Git on Windows you have two options; using msysgit or Cygwin. Using msysgit is the easiest way and probably the most popular one, but it does not allow you to use the latest version of Git as soon as it’s released. This is one of many reasons I prefer Cygwin over msysgit but it shouldn’t be any limitation if you prefer one option over the other. Since I’m a Cygwin user, it was a natural choice for me, so fell free to chose the one that suits you best.

That said, I’ll show you how to install Git v1.7.0 using Cygwin v1.7.1-1 on Windows 7.

Installing Cygwin

Download Cygwin here and run the setup. Accept the defaults (or customize it if you prefer to install in different place) until you get to the “Select Packages” step.

To select a package, change the “View” to full, type the name in the search box and press the word “Skip” once. This will select the last available version and all the required package dependencies as shown in the picture:

Select Packages

Select the following packages:

  • zlib
  • openssh
  • openssl
  • perl
  • subversion-perl (if you want to use git-svn)
  • curl
  • libcurl-devel
  • expat
  • tcltk
  • make
  • gcc
  • ncurses (if you want to use the clear command)
  • python

Click “Next” to download and install. After installation, check the option to add an icon to the Start menu.

Installing Git

Open Cygwin bash shell (Start –> All Programs -> Cygwin -> Cygwin Bash Shell) and type the following commands to download and install Git source package:

cd /usr/src
curl -O http://kernel.org/pub/software/scm/git/git-1.7.0.tar.gz
tar xzvf git-1.7.0.tar.gz
cd git-1.7.0
./configure --prefix=/usr/local
make
make install
cd ..
which git

You should see /usr/local/bin/git.

Next, download and install the Git man pages:

curl -O http://www.kernel.org/pub/software/scm/git/git-manpages-1.7.0.tar.gz
mkdir /usr/local/man
tar xvf git-manpages-1.7.0.tar.gz -C /usr/local/man

That’s all. In the next article I’ll write about how to configure Git to integrate with tools like Notepad++ and Beyond Compare.

Shout it

How to convert WinForms Bitmap to WPF ImageSource

If by chance you’re progressively migrating a legacy WinForms application to WPF, at some point you’ll have to use existing bitmaps and convert them to the WPF equivalent one.

It seems this is a FAQ in the forums, and some of the solutions that I saw that use the System.Windows.Interop namespace, have memory leak issues. When creating a GDI bitmap object, we must release its memory as soon as possible and people are missing this important step.

That said, I’ve decided to share the following code snippet that you can use to convert a Bitmap to an ImageSource.

public static class BitmapExtensions
{
    public static ImageSource ToImageSource(this Bitmap bitmap)
    {
        var hbitmap = bitmap.GetHbitmap();
        try
        {
            var imageSource = Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(bitmap.Width, bitmap.Height));

            return imageSource;
        }
        finally
        {
            NativeMethods.DeleteObject(hbitmap);
        }
    }
}

public static class NativeMethods
{
    [DllImport("gdi32")]
    public static extern int DeleteObject(IntPtr hObject);
}

Shout it

How to get Design Mode property in WPF

Last day I needed to check if the code was executing in Design Mode but I couldn’t remember how to do it, so I had to Google again for that matter. That said, I decided to blog about it to prevent wasting my time in a near future.

Jim Nakashima has a nice post about the subject, you can read it here, so I wont get into much detail.

The code to get the design mode property in your control looks like this:

using System.ComponentModel;

private bool IsInDesignMode
{
    get
    {
        return DesignerProperties.GetIsInDesignMode(this);
    }
}

If you don’t have a DependencyObject you can still get the property using the following:

using System.ComponentModel;

private bool IsInDesignMode
{
    get
    {
        return DesignerProperties.GetIsInDesignMode(new DependencyObject());
    }
}

or

using System.ComponentModel;

private bool IsInDesignMode
{
    get
    {
        return (bool)DesignerProperties.IsInDesignModeProperty
            .GetMetadata(typeof(DependencyObject)).DefaultValue;
    }
}

Shout it