How would you implement a git alias that executes external commands, and works from both bash and Powershell?
I currently have a gitPrune
alias set up for both bash (in my .bash_profile
) and Powershell (in profile.ps1
), which cleans up branches that are no longer needed:
# 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()} }
This way, I can gitPrune
from both git-bash (on Windows) and from Powershell. Now I'd like to move this into my .gitconfig
, to keep together with other git-related aliases. However, the same .gitconfig
will be used regardless of whether I have a Powershell console or the git-bash terminal open. How do I implement this "dual" command using the git-config "external command" syntax?
[alias]
myPrune = "! ..."
When git uses the shell to execute an alias, the shell used is a fixed compile-time option. It's generally going to be /bin/sh
.
So you don't need to worry about the results being different and can just use the syntax you had in your bash alias without worrying about powershell. To demonstrate:
$ cat .git/config [alias] shellpath = !pid=$$ && ps -o comm= $pid $ bash -c 'git shellpath' /bin/sh $ pwsh -c 'git shellpath' /bin/sh
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With