git!别名可以在bash和Powershell中使用

sfe*_*cik 6 git bash powershell git-config

你将如何实现执行外部命令的git别名,并从bash和Powershell开始工作?

我目前gitPrune为bash(在我的.bash_profile)和Powershell(在profile.ps1)中设置了一个别名,它可以清理不再需要的分支:

# bash
alias gitPrune="git remote prune origin; git branch --merged | grep -vEe '^\*| master$' | sed 's/.*/git branch -dv \0/e'"    

# Powershell
function gitPrune { git remote prune origin; git branch --merged | Select-String -NotMatch '^\*| master$' | %{git branch -dv $_.ToString().Trim()} }
Run Code Online (Sandbox Code Playgroud)

这样,我可以gitPrune从git-bash(在Windows上)和从Powershell中获取.现在我想把它移到我的.gitconfig手中,与其他与git相关的别名保持一致.但是,.gitconfig无论是否打开Powershell控制台或git-bash终端,都将使用相同的方法.如何使用git-config"external command"语法实现这个"双"命令?

[alias]
    myPrune = "! ..."
Run Code Online (Sandbox Code Playgroud)

Gri*_*vit 1

当git使用shell执行别名时,使用的shell是固定的编译时选项。一般情况下 是这样的/bin/sh

因此,您无需担心结果不同,只需使用 bash 别名中的语法即可,而无需担心 powershell。展示:

$ 猫.git/config
[别名]
    shellpath = !pid=$$ && ps -o comm= $pid

$ bash -c 'git shellpath'
/bin/sh
$ pwsh -c 'git shellpath'
/bin/sh