The Throbbing Brain

Technology musings for the modern pragmatist

Git Tips and Tricks

I’ve been using Git for quite some time and I’ve come to really enjoy it. It might be a little weird to say that I enjoy my source control system, but it makes development so much easier in so many ways. Now, I will admit that our relationship wasn’t always awesome; working with Git can have a steep learning curve. I’ve invested a lot of time in learning to use Git, so I’ve put together a few helpful tips for you.

Change your Text Editor

I love using Git from the command line, but it’s not perfect. If you’re developing in Windows, formatting your commit messages from the command line is about as enjoyable as a dead fish in the backseat of your car on a hot and humid summer day. Set Git up to use a graphical text editor and you’ll be writing better commit messages in no time.

My text editor of choice is Sublime Text 2, though this will work for any text editor you might be using.

Configure a Text Editor - Sublime Text 2
git config --global core.editor "'C:/Program Files/Sublime Text 2/sublime_text.exe' -w"
Configure a Text Editor - Notepad++
git config --global core.editor "'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -nosession -noPlugin"

You are writing good commit messages right?

Use a Graphical Merge Tool

Now that you have a graphical text editor, why not use a diff/merge tool that you’ll be productive with? There are a lot of options here, and today I’m using DiffMerge.

When I set up my diff/merge tool I tend to just edit my .gitconfig file directly since it’s faster for me. If you want to add all this crap in via individual commands in Git that’s cool, but my recommendation is to save some time and just edit this by hand.

Configure Git to use DiffMerge
    tool = diffmerge
    guitool = diffmerge
    keepBackup = false
    trustExitCode = true
[difftool "diffmerge"]
    name = DiffMerge
    path = \"C:/Program Files/SourceGear/Common/DiffMerge/sgdm.exe\"
    cmd = sgdm --nosplash \"$LOCAL\" \"$REMOTE\"
    tool = diffmerge
    guitool = diffmerge
    prompt = false
    keepBackup = false
    keepTemporaries = false
    trustExitCode = true
[mergetool "diffmerge"]
    name = DiffMerge
    path = \"C:/Program Files/SourceGear/Common/DiffMerge/sgdm.exe\"
    cmd = sgdm --nosplash --merge --result=\"$MERGED\" \"$LOCAL\" \"$BASE\" \"$REMOTE\"

The right Diff/Merge tool makes all the difference when working on a project where you can have conflicts that need addressing.

Take some time and play around with some of the different tools and setting them up to work the way that makes sense for how you work. Today I’m using DiffMerge and in the past I’ve used WinMerge; both are great. But, with that in mind, I’ve been taking a serious look at Semantic Merge and it looks to be really promising.

Git Aliases Will Set You Free

Using Git from the command line can be both liberating and daunting. Once you appreciate the power that some of the more verbose commands give you, it is easy to get overwhelmed if you’re not super comfortable on the command line. When you start digging into the formatting functionality of “log” you’ll quickly find yourself looking for a better way to reuse several log formatting options.

For example, one of the more common log formats I use:

Creating a Log Alias for Git
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative

I’m pretty sure I found this somewhere on Stack Overflow, and while it makes reading the log nicer, who wants to type that in every time?

Oh Aliases, Yer the Best!

For the log example above, I configured an alias of “lg” that aliases the command with all the formatting so I don’t have to type it every time. Now, whenever I want that specific log format, I just type in “git lg” and blamo, a pretty log for me to look at.

Setting up an alias couldn’t be easier. For example, to create an alias of “co” that aliases the checkout command you’d type the following into your shell:

Creating a Git Alias for Checkout
git config checkout

And you have an alias; Not terribly complicated. Using it is even simpler, just type in “git co” rather than “git checkout” and you’re good to go.

There’s a rub though; for long commands that have ticks and quotes and such, I find that I mess up the syntax creating aliases like this more often than not. I find this irritating because I’m impatient and lazy and I don’t like fighting to get this type of stuff to work.

So I cheat…

Whenever I need to edit my config file I just edit it directly from my C:\Users\yournamehere directory and in the case of aliases, edit the [alias] section. Not hard, and personally I’d rather edit it this way. Powershell is sweet, but a text editor it is not.

To get you started I’ve created a gist of a few commands that I use frequently. Start with these, make your own, and search for other aliases to make your command line Git workflow just that much better.

Streamline Git With Powershell

Using Git with Powershell, Posh-Git and Console2

You’ve installed the latest version of Git, read some tutorials and have been merrily branching, commiting and having a jolly old time enjoying the freedom that Git gives you. At some point Git “clicked”, and you picked up your laptop to show a co-worker how bad ass this new thing you did was; It was probably when you did your first reintegration merge without angering the gods.

Yet, with all the power that Git bestows, you’re still missing out. Let’s face it, Git is cool. But if you’re using the default command line environment on Windows, it feels a bit… meh.

So what are we missing? Consider for a moment…

  • Out of the box, you can use Git Bash or cmd.exe (not great options imo)
  • You have no “real” ability to customize cmd.exe
  • Scripting is horrible (by today’s standards)
  • Out of the box you can use Powershell, but without customization you’re really only getting a nicer shell

To get past the limitations of the default command line experience, we’re going to set a few things up that make Powershell shine when working with Git.

Cracking Visual Source Safe Passwords

VSS Repository… really?… REALLY?!

Several months ago I was approached by a project manager asking for help with some truly legacy code; stuff that was so old it existed only in an ancient source safe database that hadn’tseen the light of day in years. Unfortunately no one had (or remembered) credentials to get in and this project update wasn’t going away any time soon. We needed a solution to crack the database and get access to our client’s code. Needless to say, this was one of the last projects I wanted to work on.

VSS is known to have a crappy security implementation, so I took it as a challenge to write my own password cracker based on some sparse interwebs info, and my own penetration testing. Needless to say, the rumors of the lax security model implemented in VSS are true. This started out with a client need, and ended up turning into a fun little security project.

The goals that I set for myself were pretty simple:

  1. Leverage a simple command line interface
  2. Crack the passwords for one or more users
  3. Exploit VSS custom hash functionality
  4. Export the cracked passwords to a text file

You. Poor. Bastard.

The now aptly named You Poor Bastard is pretty straight-forward. The following command via CMD/Powershell

.\YouPoorBastard.exe -p "C:\SomeDataFolder\YourVSSDirectory" -e "C:\SomeDataFolder\Export.txt"

Will iterate all the users in the database and export the resulting passwords to a tab separated file.

You can also generate passwords for a single user and print them to the screen rather than a file. It’s not the fanciest thing I’ve ever written, but when you need a tool like this, you really need it.

Drop on over to github and check it out! With any luck, you’ll never need to actually use this utility.

Setting Up a Home NAS

There was a time when I was more than happy to sit inside on a warm summer weekend with some spare computer hardware and a healthy amount of nerdy ambition.  Those days were filled with triumph, frustration and more than a little swearing as a finger would be neatly splayed open by the sharp metal edge of a computer case.

Then one day I woke up and realized I had a lot of computers.  By “a lot”, I mean more than even a developer really needs.  Enough that it was noticeable on my electric bill, my home office sounded like an airport and was perpetually 10 degrees warmer than any other room in my house. Anyone who has this many machines knows the sense of loss when power goes out long enough for the UPS’s to die and all your little fans blow their last breath. The silence is deafening.

Shortly thereafter I decided I needed a home network that wasn’t a part time job to maintain and set out to simplify things.  First thing to go?  The mail server; replaced by Google Apps and my own domain.  Next were the web and database servers; replaced by virtualization powered by VMWare Workstation.  Now I’m down to a single computer and with the exception of the file server, my beloved Alienware M15x happily performs the duties of my old hardware, albeit in a much different way.