Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git !alias that would work in both bash and Powershell

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 = "! ..."
like image 596
sferencik Avatar asked Jul 05 '18 09:07

sferencik


1 Answers

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
like image 119
Grisha Levit Avatar answered Oct 13 '22 01:10

Grisha Levit