Better Git with PowerShell

I’m usually not one to resort to puns in my blog titles, but I couldn’t resist. Git it? Git it? Sorry.

Ever since we introduced PowerShell into NuGet, I’ve become a big fan. I think it’s great, yet I’ve heard from so many other developers that they have no time to try it out. That it’s “on their list” and they really want to learn it, but they just don’t have the time.

But here’s the dirty little secret about PowerShell. This might get me banned from the PowerShell junkie secret meet-ups (complete with secret handshake) for leaking it, but here it is anyways. You don’t have to learn PowerShell to get started with it and benefit from it!

Seriously. If you use a command line today, and switch to PowerShell instead, pretty much everything you do day to day still works without changing much of your workflow. There might be the occasional hiccup here and there, but not a whole lot. And over time, as you use it more, you can slowly start accreting PowerShell knowledge and start to really enjoy its power. But on your time schedule.

UPDATE: Before you do any of this, make sure you have Git for Windows (msysgit) installed. Read my post about how to get this set up and configured.

There’s a tiny bit of one time setup you do need to remember to do:

Set-ExecutionPolicy RemoteSigned

Note: Some folks simply use Unrestricted for that instead of RemoteSigned. I tend to play it safe until shit breaks. So with that bit out of the way, let’s talk about the benefits.

Posh-Git

If you do any work with Git on Windows, you owe it to yourself to check out Posh-Git. In fact, there’s also Posh-HG for mercurial users and even Posh-Svn for those so inclined.

Once you have Posh-Git loaded up, your PowerShell window lights up with extra information and features when you are in a directory with a git repository.

posh-git-info

Notice that my PowerShell prompt includes the current branch name as well as information about the current status of my index. I have 2 files added to my index ready to be committed.

More importantly though, Posh-Git adds tab expansions for Git commands as well as your branches! The following animated GIF shows what happens when I hit the tab key multiple times to cycle through my available branches. That alone is just sublime.

ps-tab-expansion

Install Posh-Git using PsGet

You’re ready to dive into Posh-Git now, right? So how do you get it? Well, you could follow all those pesky directions on the GitHub site. But we’re software developers. We don’t follow no stinkin’ list of instructions. It’s time to AWW TOE  MATE!

And this is where a cool utility named PsGet comes along. There are several implementations of “PsGet” around, but the one I cover here is so dirt simple to use I cried the first time I used it.

To use posh-git, I only needed to run the following two commands:

(new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") | iex
install-module posh-git

Here’s a screenshot of my PowerShell window running the command. Once you run the commands, you’ll need to close and re-open the PowerShell console for the changes to take effect.ps-installing-posh-gitThat’s

Both of these commands are pulled right from the PsGet homepage. That’s it! Took me no effort to do this, but suddenly using Git just got that much smoother for me.

Many thanks to Keith Dahlby for Posh-Git and Mike Chaliy for PsGet. Now go git it!

What others have said

Requesting Gravatar... NotMyself Dec 13, 2011 3:32 AM
# re: Better Git with PowerShell
Now this is content I can get behind!
Requesting Gravatar... Jason Punyon Dec 13, 2011 3:33 AM
# re: Better Git with PowerShell
Have they fixed the quote problems in PS yet? I'm remember problems running things like osql...
Requesting Gravatar... Gabriel Perez Dec 13, 2011 3:40 AM
# re: Better Git with PowerShell
Dude, your move to GitHub is already paying off big time. I was actually just in the middle of doing some powershell work with git. This post definitely came in the right time.

Thanks Phil!

Ah, one off topic question: now that you're a remote worker, have you created a Virtual Phil at GitHub like Hanselman does at Microsoft? :)
Requesting Gravatar... haacked Dec 13, 2011 3:58 AM
# re: Better Git with PowerShell
@Gabriel there's really no need. Everything they do is in CampFire chat. Even when they sit next to each other! :)
Requesting Gravatar... Matt Andreko Dec 13, 2011 3:58 AM
# re: Better Git with PowerShell
I tried installing both posh-hg and posh-git on the same machine, since I use both, and ran into some problems. However, this blog post seemed to point me in the right direction:
joseoncode.com/...
Requesting Gravatar... Will Gant Dec 13, 2011 4:03 AM
# re: Better Git with PowerShell
Holy cow Phil. This is going to take a lot of the suck out of working with git. And it might just get me motivated enough to get off my butt and learn how to use Powershell all proper-like. Very cool. Thank you very much.
Requesting Gravatar... Khalid Abuhakmeh Dec 13, 2011 4:16 AM
# re: Better Git with PowerShell
I work in both Hg and Git repositories, so I like to have both of these plugins installed at the same time. Check out my question on Stackoverflow to figure out how to do it.

stackoverflow.com/...

Requesting Gravatar... Konstantin Tarkus Dec 13, 2011 4:56 AM
# re: Better Git with PowerShell
Great hint! Thanks Phil. I am wondering are there any possible issues when using Posh-Git instead of Git Bash?
Requesting Gravatar... haacked Dec 13, 2011 4:59 AM
# re: Better Git with PowerShell
@Konstantin I haven't had any problems. I used Git-Bash to set up my ssh keys and then I never use it again and do everything in PowerShell. :)
Requesting Gravatar... Mark Dec 13, 2011 5:10 AM
# re: Better Git with PowerShell
Phil, say it ain't so! Along with your move you've lost the "Hendrix with 10,000 volts" gravatar?
Requesting Gravatar... haacked Dec 13, 2011 5:12 AM
# re: Better Git with PowerShell
@Mark that was Sho Nuff, not Hendrix! haacked.com/.../who-is-the-master.aspx
Requesting Gravatar... Dan Press Dec 13, 2011 5:21 AM
# re: Better Git with PowerShell
How do you install Git on Windows in the first place? The last time I did (it has been a year or more), I think I tried "msysgit" and had nothing but problems (like, it changed my system fonts and never worked properly).

I have used Mercurial ever since, but would like to try Git. Is there a better way to install it without screwing up Windows?
Requesting Gravatar... Rasmus Christensen Dec 13, 2011 5:29 AM
# re: Better Git with PowerShell
Well just ran both commands, but when I restart powershell and navigate to a GIT repository, there is no extra information. If I run a GIT status I get the information similar to using the GIT bash...any clue?
Requesting Gravatar... KK Dec 13, 2011 5:30 AM
# re: Better Git with PowerShell
anyway to improve tab expansions speed ?
Requesting Gravatar... haacked Dec 13, 2011 5:36 AM
# re: Better Git with PowerShell
@Rasmus Do you have a proifle script named Microsoft.Powershell_profile.ps1? Mine is at C:\Users\Haacked\Documents\WindowsPowerShell\Microsoft.Powershell_profile.ps1
Requesting Gravatar... Rasmus Christensen Dec 13, 2011 5:54 AM
# re: Better Git with PowerShell
Nope. I just tried joseoncode.com/... and got the missing posh-hg.

Nut I could try to remove it again and add a profile as you describe. Whats in it?
Requesting Gravatar... Paul Betts Dec 13, 2011 6:42 AM
# re: Better Git with PowerShell
Everyone needs to know about PsGet, it's full of awesomeness and awesomocity!
Requesting Gravatar... Sergey Shishkin Dec 13, 2011 7:21 AM
# re: Better Git with PowerShell
Please do your eyes a favor and install Console2 with a better color scheme and fonts.
Requesting Gravatar... Russ Cam Dec 13, 2011 7:27 AM
# re: Better Git with PowerShell
@Rasmus - I was having similar issues. I have Mercurial and Git installed (but not posh-hg, as far as I know). In %userprofile%\Documents\WindowsPowerShell, create a profile.ps1 powershell file and add the code from joseoncode.com/...
I just commented out the line beginning

. $Home\Documents\WindowsPowerShell\Modules\posh-hg\profile.example.ps1

by prefixing with a # and now all is working as expected.
Requesting Gravatar... José f. Romaniello Dec 13, 2011 9:45 AM
# re: Better Git with PowerShell
Thanks everyone for the references to my workarround, i think we need a better solution though. Installing both should not require this extra steps....

I'd have a deeper look.
Requesting Gravatar... Jason Jarrett Dec 13, 2011 10:22 AM
# re: Better Git with PowerShell
You can also use chocolatey to install poshgit. "chocolatey install poshgit" or "cinst poshgit".
Requesting Gravatar... Stacy Dec 13, 2011 10:43 AM
# re: Better Git with PowerShell
What's the best way to get started with git and vs2010? Currently I'm using visualsvn which is well integrated and works great.
Requesting Gravatar... Christian Abildsø Dec 13, 2011 4:34 PM
# re: Better Git with PowerShell
I installed posh-git, but it seems to lag a couple of seconds for every prompt before it displays the branch name. There is also a noticeable delay (not as long) for the tab completion for git commands/branch names/etc. With the git bash this is instant. I already use Console 2, so I always have both shells readily available, but it would be nice to be able to do everything in one shell. Has anyone noticed this slowdown as well and know what it's caused by?
Requesting Gravatar... Filip Kinsky Dec 13, 2011 4:38 PM
# re: Better Git with PowerShell
@Stacy: try Git Source Control Provider (it can be installed through VS Tools>Extension Manager) - it's quite simple, but it can handle the basic git operations directly from VS Pending Changes window etc..
Requesting Gravatar... Christian Abildsø Dec 13, 2011 4:44 PM
# re: Better Git with PowerShell
Ah, I guess I needed to RTFM. Apparently posh-git suffers slowdowns with large repositories. Our project is around 700kloc and has 11-12 years of history that has been imported into git from SVN and SourceSafe. The git bash has no such delays, so I guess I'm back to using that. Appreciate the tip none-the-less though. :)
Requesting Gravatar... Matt Dec 13, 2011 5:35 PM
# re: Better Git with PowerShell
@Stacy - GitExtensions
Requesting Gravatar... George Dec 13, 2011 7:06 PM
# re: Better Git with PowerShell
That's very nice, unfortunately many .NET developers are afraid to use command-line tools (including Powershell), so won't use a new source control system like Git unless it has a very good Visual Studio integration like TFS has...
That's the unfortunate reality, and that's why many didn't use even SVN, which had some VS integration, but was quite broken 5 years ago (AnkhSVN), while VisuaSVN was not free, so was out of reach for many.
Requesting Gravatar... Barry Dec 13, 2011 9:04 PM
# re: Better Git with PowerShell
Nice. If you add posh-hg and posh-svn you have to repeat the psget command and then manually add them to your profile, which is just a case of 'notepad $PROFILE'.
Requesting Gravatar... Chris Chandler Dec 13, 2011 10:56 PM
# re: Better Git with PowerShell
Do you have any perf problems with posh-git. Takes about 10 seconds for the branch name and status stuff to come up.

Where as, I have a hand rolled prompt, no tab completion. That is much much much faster. Thoughts?
Requesting Gravatar... Chris Chandler Dec 13, 2011 11:07 PM
# re: Better Git with PowerShell
@christian I can show you my prompt, which is just as fast as git bash, on our very large repo. I just parse the branch list call to get branch and don't bother with recursion, if its not a git repo. The branch call returns an error, and its much faster than seeking a .git dir.
Requesting Gravatar... Bryan Dec 14, 2011 12:32 AM
# re: Better Git with PowerShell
Good article as per usual, but why be cussing? There's no utility in it. No one goes to a movie thinking, "I sure hope there's lots of swearing. I'll feel cheated otherwise." I'm calling out the emperors.
Requesting Gravatar... Scott Koon Dec 15, 2011 3:34 AM
# re: Better Git with PowerShell
I didn't like PoshGit. Had some performance issues with it. I found this combined Hg and Git script and I've been using it for a few months with no problem.

matthewmanela.com/...
Requesting Gravatar... Scott Koon Dec 15, 2011 3:36 AM
# re: Better Git with PowerShell
Oh, the machine I ran PoshGit on and saw slowness on is a I7 3.40GHz quadcore machine with 16GB of RAM. :/
Requesting Gravatar... Scott Muc Dec 16, 2011 3:00 AM
# re: Better Git with PowerShell
Yes! I love PSGet and hope that it can grow to be the standard for PowerShell module management.
Requesting Gravatar... Eric Falsken Dec 18, 2011 3:56 AM
# re: Better Git with PowerShell
I love the idea of Git, but where can I find a "Git-ing Started for mere .NET developers?"
Requesting Gravatar... Sam Dec 18, 2011 11:07 AM
# re: Better Git with PowerShell
Tried it with msysgit 1.7.5. Got a warning:

"WARNING: posh-git required msysgit version 1.7. You have ."

Great error message. Doesn't seem to have done anything, prompt isn't branch aware etc. Maybe you could share what git distro u are using Phil?
Requesting Gravatar... Sam Dec 18, 2011 11:31 AM
# re: Better Git with PowerShell
Update: I changed to portable msysgit and all is well. Not having problems with slow tab completion either.
Requesting Gravatar... CarrierIQ-Lover Dec 19, 2011 6:42 AM
# re: Better Git with PowerShell
I just wanted to point out that both this article and the install directions at github for the posh-git project both leave out the dependency you need to get any of this running in the first place.

To those already familiar with using git on windows this might be implicit but to those frustrated others, this is a glaring omission. I suggest that your article be updated with the required git dependency.

As always, thanks for you insight, dedication and hard work, myself and the community at large appreciate it!
Requesting Gravatar... Duncan Apr 10, 2012 2:10 PM
# re: Better Git with PowerShell
I was having some trouble getting poshgit to actually run which I think may have been due to missing a dependency.

I reinstalled using chocolatey as suggested by Jason Jarrett and now it works fine.

Thanks!

What do you have to say?

(will show your gravatar)
Please add 3 and 8 and type the answer here: