在PowerShell中使用conda init后,Conda环境名称会隐藏git分支

6

我已经安装了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分支。如何修改此设置以便同时查看两者?


1
也许可以尝试调整Conda的env_prompt配置变量。使用conda config --describe env_prompt命令查看文档。 - merv
@merv 这只是更改conda显示conda特定前缀。它不会停止阻止我的git分支。 - Woody1193
是的,我想可能是Conda插入的某些东西可能出了问题。 - merv
1
@merv 看起来不是。我猜想conda init中使用的脚本正在拦截posh-git的输出,但我不确定。 - Woody1193
1个回答

9

posh-git的文档说明posh-git不会修改自定义提示符。因此,当您在conda初始化后运行Import-Module posh-git时,您将不会看到任何变化。解决方法是将Import-Module posh-git放在conda初始化块之前,这样您的$Profile.CurrentUserAllHosts文件就应该是这个样子:

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 之后执行的,所以那个命令将不会有任何明显的效果。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接