Conda 环境名称在 Powershell 中的 conda init 之后隐藏 git 分支

Conda environment name hides git branch after conda init in Powershell

我为 Powershell 安装了 Posh-Git 模块,最近,我还安装了 Anaconda 并做了 conda init。显然,这通过添加以下代码修改了 profile.ps1 文件:

#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
(& "{User}\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression
#endregion

这确实让我看到了我正在使用的 conda 环境,但它也隐藏了我正在使用的 Git 分支。我该如何修改才能同时看到两者?

posh-git docs 声明 posh-git 不会修改自定义提示。因此,当您 运行 Import-Module posh-git 在 conda 初始化后,您将看不到任何变化。解决方案是将 Import-Module posh-git 放在 $Profile.CurrentUserAllHosts 文件中的 conda 初始化块之前,这样它看起来像:

Import-Module posh-git

#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
(& "{User}\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression
#endregion

还值得注意的是,当您 运行 Add-PoshGitToProfile 时,它会将 Import-Module posh-git 行添加到您的 $Profile.CurrentUserCurrentHost 文件中。由于此命令在 $Profile.CurrentUserAllHosts 之后执行 ,因此该命令不会产生任何明显的效果。