A PowerShell proxy command around Set-Location
– and thus its built-in aliases cd
, chdir
, and sl
– to defer import of the posh-git module until one changes the working directory to the root of a git directory. This is useful for speeding up PowerShell startup times so that one does not have to import the posh-git
module itself from the profile.
Install the module from the PowerShell Gallery:
Install-Module -Name 'lazy-posh-git' -AllowClobber
Then add this line to your PowerShell $PROFILE
:
Import-Module 'lazy-posh-git'
Only once you navigate to a git directory is the posh-git
module imported, thereby defering the delay caused by the import to when it is actually needed.
If there are certain posh-git
settings you wish to change after it is imported or when running a custom script after import, you can pass the path to a script as the first argument to the PoshGitSettingsScriptPath
parameter when importing lazy-posh-git
:
Import-Module 'lazy-posh-git' -ArgumentList '~/path-to-my/git-prompt-settings.ps1'
lazy-posh-git
will run this script immediately after it has imported posh-git
. The path could be absolute or relative, depending on the assumptions you can make. For example, your script could look like this:
$GitPromptSettings.DefaultPromptAbbreviateHomeDirectory = $true
$GitPromptSettings.AfterStatus.Text = "]`n"
This module was built by exporting creating the base of the proxy command with the following script:
$metaData = New-Object System.Management.Automation.CommandMetaData(Get-Command Set-Location)
[System.Management.Automation.ProxyCommand]::Create($metadata)
This command is then wrapped in a function as function{ }
and code added at the end of its end
block to add the custom logic for importing posh-git
if there is a .git
folder in the current working directory.
This early version works for PowerShell 5.1 and 7. If you need support for older versions, please open an issue or a pull request.