I want to create a Git alias to perform multiple commands, but I cant find documentation on how this is done.
I struggled to find a nice example for myself too. After some research, here's what I came up with:
git config --global alias.purge '!f() { if [ $(git ls-remote --heads origin $1 | wc -l) -eq 1 ]; then git branch -d $1 && git push origin --delete $1; else echo "remote branch not found"; fi } ; f'
Let's break that down from left to right, shall we?
git config --global alias. we know by now. It's the code we need in order to create an alias. Often referred to as [alias] on this site
purge is the shorthand name I decided to give to my alias
'f() { we start to write our function
if [ the start of our if-statement, closing it later on with ]
git ls-remote --heads origin $1 checks if there is a remote branch with the name we supplied. Return value would be nothing if there's no branch, otherwise, it'll return the reference tag. By enclosing it in $( ) we make sure it's known to Bash as a command
| wc -1 added as a suffix, converts the return value to 0 if there's no remote branch, 1 if there is
-eq 1 turns the whole enclosed command into a boolean, read as (returnValue == 1)
NOTE: because the commands are written on one line, instead of \n behind each, we need to type ; after each command
then is used to announce what our function should do if the boolean returns true
git branch -d $1 will delete the local branch of the parameter we entered
&& this logical operator will ensure both commands are carried out
git push origin –delete $1 will delete the remote branch of the parameter we entered
else does what the second half of an if-else should do
echo "remote branch not found" is a String I want to return in case there's no remote branch
fi announces the end of our if-else statement
} ; f' closes our function