Better Git with PowerShell

code, git 0 comments suggest edit

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 directionson 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!

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.

Comments

avatar

51 responses

  1. Avatar for NotMyself
    NotMyself December 13th, 2011

    Now this is content I can get behind!

  2. Avatar for Jason Punyon
    Jason Punyon December 13th, 2011

    Have they fixed the quote problems in PS yet? I'm remember problems running things like osql...

  3. Avatar for Gabriel Perez
    Gabriel Perez December 13th, 2011

    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? :)

  4. Avatar for haacked
    haacked December 13th, 2011

    @Gabriel there's really no need. Everything they do is in CampFire chat. Even when they sit next to each other! :)

  5. Avatar for mandreko
    mandreko December 13th, 2011

    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/...

  6. Avatar for Will Gant
    Will Gant December 13th, 2011

    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.

  7. Avatar for Khalid Abuhakmeh
    Khalid Abuhakmeh December 13th, 2011

    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/...

  8. Avatar for Konstantin Tarkus
    Konstantin Tarkus December 13th, 2011

    Great hint! Thanks Phil. I am wondering are there any possible issues when using Posh-Git instead of Git Bash?

  9. Avatar for haacked
    haacked December 13th, 2011

    @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. :)

  10. Avatar for Mark
    Mark December 13th, 2011

    Phil, say it ain't so! Along with your move you've lost the "Hendrix with 10,000 volts" gravatar?

  11. Avatar for haacked
    haacked December 13th, 2011

    @Mark that was Sho Nuff, not Hendrix! haacked.com/.../who-is-the-master.aspx

  12. Avatar for Dan Press
    Dan Press December 13th, 2011

    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?

  13. Avatar for Rasmus Christensen
    Rasmus Christensen December 13th, 2011

    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?

  14. Avatar for KK
    KK December 13th, 2011

    anyway to improve tab expansions speed ?

  15. Avatar for haacked
    haacked December 13th, 2011

    @Rasmus Do you have a proifle script named Microsoft.Powershell_profile.ps1? Mine is at C:\Users\Haacked\Documents\WindowsPowerShell\Microsoft.Powershell_profile.ps1

  16. Avatar for Rasmus Christensen
    Rasmus Christensen December 13th, 2011

    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?

  17. Avatar for Paul Betts
    Paul Betts December 13th, 2011

    Everyone needs to know about PsGet, it's full of awesomeness and awesomocity!

  18. Avatar for shishkin
    shishkin December 13th, 2011

    Please do your eyes a favor and install Console2 with a better color scheme and fonts.

  19. Avatar for Russ Cam
    Russ Cam December 13th, 2011

    @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.

  20. Avatar for José f. Romaniello
    José f. Romaniello December 13th, 2011

    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.

  21. Avatar for staxmanade
    staxmanade December 13th, 2011

    You can also use chocolatey to install poshgit. "chocolatey install poshgit" or "cinst poshgit".

  22. Avatar for Stacy
    Stacy December 13th, 2011

    What's the best way to get started with git and vs2010? Currently I'm using visualsvn which is well integrated and works great.

  23. Avatar for Christian Abildsø
    Christian Abildsø December 13th, 2011

    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?

  24. Avatar for Filip Kinsky
    Filip Kinsky December 13th, 2011

    @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..

  25. Avatar for Christian Abildsø
    Christian Abildsø December 13th, 2011

    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. :)

  26. Avatar for Matt
    Matt December 13th, 2011

    @Stacy - GitExtensions

  27. Avatar for George
    George December 13th, 2011

    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.

  28. Avatar for Barry
    Barry December 13th, 2011

    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'.

  29. Avatar for Chris Chandler
    Chris Chandler December 13th, 2011

    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?

  30. Avatar for Chris Chandler
    Chris Chandler December 13th, 2011

    @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.

  31. Avatar for Bryan
    Bryan December 13th, 2011

    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.

  32. Avatar for Scott Koon
    Scott Koon December 15th, 2011

    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/...

  33. Avatar for Scott Koon
    Scott Koon December 15th, 2011

    Oh, the machine I ran PoshGit on and saw slowness on is a I7 3.40GHz quadcore machine with 16GB of RAM. :/

  34. Avatar for Scott Muc
    Scott Muc December 16th, 2011

    Yes! I love PSGet and hope that it can grow to be the standard for PowerShell module management.

  35. Avatar for Eric Falsken
    Eric Falsken December 18th, 2011

    I love the idea of Git, but where can I find a "Git-ing Started for mere .NET developers?"

  36. Avatar for Sam
    Sam December 18th, 2011

    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?

  37. Avatar for Sam
    Sam December 18th, 2011

    Update: I changed to portable msysgit and all is well. Not having problems with slow tab completion either.

  38. Avatar for CarrierIQ-Lover
    CarrierIQ-Lover December 19th, 2011

    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!

  39. Avatar for Duncan
    Duncan April 10th, 2012

    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!

  40. Avatar for Moiz
    Moiz August 12th, 2012

    This is a very good post... no need to use the git bash shell any longer... we can now have the power of command complete of git power shell along with familiar windows shell commands...

  41. Avatar for mary
    mary December 14th, 2012

    I had all this working at some point, but it has started asking me for my password with every push/pull again. is there a way to uninstall maybe and start over? I tried resetting my keys to, all to no avail.

  42. Avatar for Ody Mbegbu
    Ody Mbegbu April 12th, 2013

    OMG.. I could kiss you right now.. :-P I have been trying to figure this out by reverse engineering the prompt in Github for windows. you have just made my day.

    http://superuser.com/questi...

  43. Avatar for Naresh Mittal
    Naresh Mittal August 7th, 2013

    Thanks for saving lot of my time. After spending lot of time on finding a suitable solution, yours is the best.

  44. Avatar for Adam The Automator
    Adam The Automator December 7th, 2013

    I'm a system administrator that uses Powershell extensively for admin tasks. How prevantly is Powershell used for developers?

    - Adam
    adamtheautomator.com

  45. Avatar for Rogue
    Rogue April 29th, 2014

    E:\src> install-module posh-git
    E:\src> . $PROFILE
    E:\src> git status
    The term 'git' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.At line:1 char:1
    + git status
    + ~~~
    + CategoryInfo : ObjectNotFound: (git:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    PowerShell is *still* the poor man's bash.

  46. Avatar for Rasmus Bækgaard
    Rasmus Bækgaard February 8th, 2015

    " Once you run the commands, you’ll need to close and re-open the PowerShell console for the changes to take effect."
    Call '.$PROFILE'. that will do it for you.

  47. Avatar for steve
    steve March 8th, 2015

    Hi Phil. I'd look to see if there's any existing issues related to this in CodePlex to see if others have interest in this.

    NuGet takes advantage of a lot of .NET 4 features and it'd be a lot of work to backport it and maintain both ports. So I don't anticipate the NuGet team wanting to pull that back into NuGet. But you can always maintain your own fork either on CodePlex or GitHub.

  48. Avatar for OG
    OG September 9th, 2015

    "It’s time to AWW TOE MATE!" I need a shirt that says this.

  49. Avatar for Wilson Mar
    Wilson Mar September 30th, 2016

    How can I tell if a machine already has posh installed? Just by looking at the command line prompt?

  50. Avatar for haacked
    haacked September 30th, 2016

    Yeah. When you change the directory to a git repository, do you see extra information such as which branch you're on? If not, then Posh-Git is not loaded.

  51. Avatar for FriKrtti
    FriKrtti August 11th, 2017

    Thanks, very helpful, use Set-ExecutionPolicy RemoteSigned if cannot able to run scripts on machine

    try ConEmu with this