My Why and How of posh-git: Context Menu, Transcripts, and TF.exe
2018-05-14 13:28
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.
- Open Powershell. Check the execution policy with Get-ExecutionPolicy. It should be RemoteSigned or Unrestricted1.
- cd ~\Documents and create directory GitHub.
- cd GitHub
- 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.
- Open Documents and create a directory WindowsPowerShell if necessary.
- 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.
- Open Visual Studio > Tools > Options > Environment > Command Line
- In "Select preset", choose Custom.
- Change "Friendly name" to something like "My posh-git"
- Command =
powershell.exe
- Command arguments =
-NoExit -ExecutionPolicy RemoteSigned -File C:\users\charl\Documents\WindowsPowerShell\posh-git.profile.ps1
- 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:
- Open the console by right-clicking an Explorer folder and choosing from the menu.
- Open the console by using Mads Kristensen's Open Command Line extension.
- Be able to run TFS commands (
tf.exe
)2. - Copy/paste to/from the console easily, i.e. CTRL-C, CTRL-V.
- Open an explorer window into my current folder using
explorer .
(note the dot after the command, which means "open here".) - Dynamically resize the console contents.
- Scroll back through commands.
- Powerful command line if needed.
References
posh-git
- posh-git Github Repo
- What's neat about this one is creating transcripts of your Git sessions.
- posh-git: Customizing Your PowerShell Prompt
- Prompts and Directories - Even Better Git (and Mercurial) with PowerShell
- Visualizing your real-time blood sugar values AND a Git Prompt on Windows PowerShell and Linux Bash
- Better Git with PowerShell
Creating Context Menu Entry
- How add context menu item to Windows Explorer for folders
- Enhancing the Open Command Prompt Here Menu
- How to use reg_expand_sz from the command line
- Managing Windows Registry from the Command Prompt
TFS from Powershell
- Adding TFS to you Powershell Commandline
- TFS Cmdlets GitHub
- Installing TFS PowerShell Cmdlets
- Powershell and TFS: The Basic and Beyond
- tf.exe Promoting Candidate Files (cannot be done)
- Using Local workspaces – Promote Excluded changes with TFS 2012 API
Git-Bash and/or Bash on Windows (they're not the same)
- Have any of you switched to Bsh on Windows?
- On Windows, PowerShell vs. Bash comparison gets interesting
Miscellaneous
- All Things Git Podcast
- If you want your Powershell windows to always be the "noble" colors, you can do this. I tried it, but then realized it got in my way.
Powershell's Noble Blue - Fail to extract PROGRAMFILES(X86) environment variable using powershell