Configuring msysgit for improved awesome

Git, as many of us know, is pretty great. And it works with great things like github and gerrit. I’ve been using it for work lately and like it very much. But the windows toolchain leaves something to be desired.

It turns out that the first rule of git on windows is don’t use cygwin git. I didn’t understand why this was until I started dealing with large repositories and doing a lot of waiting. git status took 15 seconds to run EVERY TIME. Luckily I figured it out and started using msysgit instead. msysgit is a pretty good port, but anybody who’s used a mac or any unix machine with a real terminal emulator will be immediately dismayed to find out that its shell runs inside the regular cmd.exe window. Which is crap.

This is how to make it not crap.

1. Configure the console
Did you know you can make cmd.exe not suck? I didn’t. It’s pretty easy.


The most important setting is “QuickEdit Mode”, which lets you select text directly without doing the brain-damaged menu selection thing. Then you can hit enter to copy it. The right mouse button pastes. Double click selects a word, just like you’d expect; this is really useful when dealing with git, since you can double click a commit hash and hit enter to quickly copy it, then right click to paste it to the command line.

The other settings I use and recommend are:

  • Font: Consolas 14pt
  • Window Size: Width=128
  • Window Position: “Let system position window” turned off, set to be the right half of my screen every time.
  • Colors: I use a light yellow background and dark foreground. This gives good contrast against the blue you see in so much colored output, unlike the default black background. BG=255,255,236, Text=0,0,0. You should configure your prompt first if you want to do this, so you don’t get stuck with invisible text.

2. Configure your prompt.
The default msysgit prompt settings are kind of crap. This is what I use in my ~/.bash_profile. NB: this is set up for a LIGHT background, so the text is BLACK. If you use this with the default colors, your text will be invisible.

function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

function proml {
  local       BLACK="\[\033[0;30m\]"
  local        BLUE="\[\033[0;34m\]"
  local         RED="\[\033[0;31m\]"
  local   LIGHT_RED="\[\033[1;31m\]"
  local       GREEN="\[\033[0;32m\]"
  local LIGHT_GREEN="\[\033[1;32m\]"
  local       WHITE="\[\033[1;37m\]"
  local  LIGHT_GRAY="\[\033[0;37m\]"
  case $TERM in
    xterm*)
    TITLEBAR='\[\033]0;\w\007\]'
    ;;
    *)
    TITLEBAR=""
    ;;
  esac

PS1="${TITLEBAR}\
$BLACK\w$GREEN\$(parse_git_branch)\
$BLACK\$ "
PS2='> '
PS4='+ '
}
proml

You can easily adjust the colors to be how you like. Note that the last color here will override the text color you set in the prefs window; that confused me at first. Also make sure your profile file is executable (chmod +x ~/.bash_profile) and starts with #!/usr/bin/bash, if you’re having any troubles.

3. Configure git a little
This isn’t specific to windows, but it makes my git experience so much smoother. I found the ‘lg’ alias on the internet and forgot where. Sorry, guy who made this first.

[merge]
        tool = winmerge
[mergetool "winmerge"]
        cmd = 'C:/Program Files/WinMerge/WinMergeU.exe' \"$MERGED\"
[color]
        diff = auto
        status = auto
        branch = auto
[alias]
        lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
        anchor = tag -f anchor
        s = status

‘git lg’ will now give you a nice colored log summary. I use ‘git anchor’ frequently before trying out something I may mess up or want to quickly revert; it makes it easy to get back to where I was without having to inspect the reflog. And finally, I use ‘git status’ so much that I just abbreviate it to ‘git s’.

4. RTFM
There are incredible amounts of excellent information about git on the web. Read them. You can look at my bookmarks over at del.icio.us for a starting point if you want.

Although there are a number of windows guis around, in various states of incompletion, you’ll be much better off if you learn to use git from the command line. Yes, this means editing commit logs in vi, if you stick with the defaults. If you can’t do this already, you should learn how. The GUI tools, as they are today, simply hide too much important stuff from you. The real power in git is understanding how the commit graph works and how it changes when you do various operations; this is something that should in your face, not hidden away. At most, ‘git gui’ is acceptable for staging things for commit. ‘gitk’ is alright as well. But I haven’t yet seen any windows-specific guis that do a good job.

10 thoughts on “Configuring msysgit for improved awesome

    • No, the file still exists in windows. ~ means your home directory, which in msysgit is just your windows user directory. (c:\Users\My Name on win7) Or if you know vi you can get at it easily with ‘vi ~/.bash_profile’

  1. How exactly you came to the conclusion to post this to an almost perfectly-hidden blog post rather than participating in the msysGit project is beyond me. You seem not to be a stupid person, judging from what you wrote here, so your decision to hide your findings is even more incredible to me.

    Just for the record: even I — being less than a dummy on the net, and with Git in particular — did not see this post without a dedicated Google Alert for the term “msysGit”.

    So how you came to decide that your time was best spent doing this post with particularly-little impact is quite beyond me.

    • I don’t know much about how the msysgit project works, I just know what works for me. Do you know the right place is to contribute documentation like this?

  2. Cmd.exe gets a lot of undeserved criticism. (It gets a lot of deserved criticism, too, but that’s another matter.) Did you know cmd.exe can do filename and directory name-completion, too (if enabled)? By default it’s ctrl+f for file names and ctrl+d for directory names, but you can change them both to tab if you want a more linuxy feel… more at http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx?mfr=true.

  3. mullr

    Thanks for the very useful information! It is much appreciated.

    (I did not have any problems finding this blog entry with a simple Google search, contrary to the comment from @Dscho above)

  4. Nice post with great info for using git on windows
    the command prompt did not work well for me – I blended it with
    http://henrik.nyh.se/2008/12/git-dirty-prompt (well I gained knowledge of colors from you snippet)

    especially grateful for the info on setting the merge tool

    here is my list of aliases

    who = shortlog -s --
    cp = cherry-pick
    st = status
    ci = commit
    cp = cherry-pick
    mf = merge
    mn = merge --no-ff
    br = branch
    co = checkout
    df = diff
    lg = log -p
    sg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative
    sl = log --graph
    ab = log --graph --pretty=oneline --abbrev-commit
    rb = rebase
    ru = remote update
    su = submodule status
    si = submodule update --init

    comprised from gitready, github and
    http://www.jukie.net/bart/blog/pimping-out-git-log

    I also make use of git flow
    https://github.com/nvie/gitflow

    I have today found other interesting things for git but not yet tested them out :)

  5. Pingback: Chmod 033 | Harrymountbatt

  6. Pingback: Git Windows Customization and Enhancement Workshop | Mastering software development

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s