I'd been pretty happy using Cmd to run my Git command line. I didn't have requirements that forced me to use something else. But I hadn't really compared the three main command-line shells for Git on Windows (Command, Powershell, Git-Bash), or thought about features I wanted or maybe didn't know I wanted.

I didn't do an extensive review, just an exploration to see whether I wanted to change my default. The answer turned out to be "yes"!

Contents

Skip to the End!

I was persuaded by all things git's Edward Thomson's argument that Windows users shouldn't use Git-Bash for the simple reason that we shouldn't have to use a foreign operating system's shell to work with Git.

I was also persuaded by Scott Hanselman and Phil Haack that using Powershell with posh-git was an awesome Windows solution. I was even more convinced by Dave Hein's article showing how to create a simple transcription method to record git sessions. That could be a lifesaver.

Here's my current setup.

Step 1 - Install Git for Windows

You can follow the defaults and be fine. Some people recommend not changing your local PATH environment variable, but I think it's OK and reduces some friction. I do have preferred configuration answers and may blog them in the future.

Step 2 - Install posh-git

These steps assume git.exe is on your path. Steps mostly copied from Dave Hein.

  1. Open Powershell. Check the execution policy with Get-ExecutionPolicy. It should be RemoteSigned or Unrestricted1.
  2. cd ~\Documents and create directory GitHub.
  3. cd GitHub
  4. git clone https://github.com/dahlbyk/posh-git.git.

Step 3 - Create a Powershell profile

Warning
Many sites that show examples of Powershell profiles for posh-git are for older versions, especially when it comes to changing the prompt.

  1. Open Documents and create a directory WindowsPowerShell if necessary.
  2. In WindowsPowerShell, use a text editor to create a file named posh-git.profile.ps1

This is my script.

    $Host.UI.RawUI.BackgroundColor = 'Black'
    $Host.UI.RawUI.ForegroundColor = 'Gray'
    Clear-Host
    
    $Env:Path = "$Env:ProgramFiles\Git\bin" + ";" + $Env:Path
    Push-Location (Resolve-Path "$Env:USERPROFILE\Documents\GitHub\posh-git")
    
    # Import the posh-git module, first via installed posh-git module.
    # If the module isn't installed, then attempt to load it from the cloned posh-git Git repo.
    $poshGitModule = Get-Module posh-git -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1
    if ($poshGitModule) {
        $poshGitModule | Import-Module
    }
    elseif (Test-Path -LiteralPath ($modulePath = Join-Path (Get-Location) (Join-Path src 'posh-git.psd1'))) {
        Import-Module $modulePath
    }
    else {
        throw "Failed to import posh-git."
    }
    
    Pop-Location
    
    # Settings for the prompt are in GitPrompt.ps1, so add any desired settings changes here.
    # Example:
    #     $Global:GitPromptSettings.BranchBehindAndAheadDisplay = "Compact"
    
    $Global:GitPromptSettings.DefaultPromptBeforeSuffix.Text = "`r`n"
    $Global:GitPromptSettings.DefaultPromptPath.ForegroundColor = 'Orange'
    
    Start-SshAgent -Quiet
    
    # Start a transcript
    #
    if (!(Test-Path "$Env:USERPROFILE\Documents\WindowsPowerShell\Transcripts"))
    {
        if (!(Test-Path "$Env:USERPROFILE\Documents\WindowsPowerShell"))
        {
            $rc = New-Item -Path "$Env:USERPROFILE\Documents\WindowsPowerShell" -ItemType directory
        }
        $rc = New-Item -Path "$Env:USERPROFILE\Documents\WindowsPowerShell\Transcripts" -ItemType directory
    }
    $curdate = $(get-date -Format "yyyyMMddhhmmss")
    Start-Transcript -Path "$Env:USERPROFILE\Documents\WindowsPowerShell\Transcripts\PowerShell_transcript.$curdate.txt"
    
    # Alias TFS command line
    Set-Alias tf "${Env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\tf.exe"

Step 4 - Add Context Menu Entry

I frequently want to open a Git console by right-clicking in the folder. Git for Windows adds that for Git-Bash (unless you say not to). Here's the registry file to add context menu entries for our new posh-git setup.

Note
You need to replace the user profile path below with your own.

Windows Registry Editor Version 5.00

# Right-click on Explorer folder's empty space in right pane
[HKEY_CLASSES_ROOT\Directory\Background\shell\011PowershellPoshGit]
@="Open with Powershell posh-git"
"Icon"="powershell.exe"

[HKEY_CLASSES_ROOT\Directory\Background\shell\011PowershellPoshGit\command]
@="powershell.exe -NoExit -ExecutionPolicy RemoteSigned -File C:\\users\\charl\\Documents\\WindowsPowerShell\\posh-git.profile.ps1"

Step 5 - Add Custom posh-git entry to Visual Studio Open Command Line Extension

I like Mads Kristensen's Open Command Line extension to Alt-Space open a command window. If you don't use it, skip this part.

  1. Open Visual Studio > Tools > Options > Environment > Command Line
  2. In "Select preset", choose Custom.
  3. Change "Friendly name" to something like "My posh-git"
  4. Command = powershell.exe
  5. Command arguments = -NoExit -ExecutionPolicy RemoteSigned -File C:\users\charl\Documents\WindowsPowerShell\posh-git.profile.ps1
  6. Save and exit

Results

You can now right-click inside a folder and open posh-git from a menu item.

Which, in my case, gives this console. I can run both Git commands and TFS commands.

I'll get the same console if I have a solution open in Visual Studio and press ALT-Space.

To Do

  • Explore installing and using the TFS cmdlets, which might improve my weird workflow, (see below).

A Summary of How I Got Here

I originally was going to make this a review/comparison of the three command lines, but by the end of my research I realized there wasn't much point (to me) in that.

Git-Bash was off the table. The only reason I could see using it was for git aliases that rely on Unix commands, and even that seemed iffy. I'd rather rewrite such alises using native Windows commands.

My requirements were:

  1. Open the console by right-clicking an Explorer folder and choosing from the menu.
  2. Open the console by using Mads Kristensen's Open Command Line extension.
  3. Be able to run TFS commands (tf.exe)2.
  4. Copy/paste to/from the console easily, i.e. CTRL-C, CTRL-V.
  5. Open an explorer window into my current folder using explorer . (note the dot after the command, which means "open here".)
  6. Dynamically resize the console contents.
  7. Scroll back through commands.
  8. Powerful command line if needed.

References

posh-git

Creating Context Menu Entry

TFS from Powershell

Git-Bash and/or Bash on Windows (they're not the same)

Miscellaneous


  1. Run PowerShell as administrator and call Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm

  2. I have a kind of crazy work flow where I use Git locally, but check in code to TFS remotely.