Environment/Background:
zsh (v5.9)
git (v2.48)
git --exec-path results in /usr/lib/git-core
Other StackOverflow posts read:
Problem
I'd like to set up an alias that prints detailed information about the branches in a repo; something that combines git branch -vv --list --all with git for-each-ref --sort=-creatordate refs/heads/ --format='%(creatordate:short) %(refname:short)'. After reading this, I came up with this:
[alias]
bls = '!f() { git branch -vv --list --all && echo \"\nSorted by creation date:\" && git for-each-ref --sort=-creatordate refs/heads/ --
format=\"%(creatordate:short) %(refname:short)\"; }; f'
That results in expansion of alias 'bls' failed; 'git' is not a git command.
I'm flummoxed because other aliases work, as well as other aliases using the !git <command> && <command> syntax. But not this syntax: '!f() { git && ; }; f'. Other examples of syntax I've tried:
[alias]
foo = '!f() { git version && echo foo; }; f' # results in error "fatal: bad alias.foo string: unclosed quote
bar = bar = "!f() { git version; echo qux; }; f" # works!
bls = "!f() { git branch -vv --list --all && echo \"\nSorted by creation date:\" && git for-each-ref --sort=\"-creatordate refs/heads/\" --format=\"%(creatordate:short) %(refname:short)\"; }; f" # error: expansion of alias 'bls' failed; 'git' is not a git command
Edit: I've gotten it to work with this answer to a similar question, but I can't help but wonder if it's possible to do this in one line. Here's my current solution:
[alias]
bls = ! " \
f () { \
git branch -vv --list --all && \
echo \"\nSorted by creation date:\" && \
git for-each-ref --sort=\"-creatordate refs/heads/\" \
--format=\"%(creatordate:short) %(refname:short)\"; \
}; \
f"
Edit 2: @AlexHowansky was right. Double quoting the full string works. The troubleshooting process was confounded by the presence of both global and locally scoped aliases with the same name. The one line version of the above would be:
bls = ! "f () { git branch -vv --list --all && echo \"\nSorted by creation date:\" && git for-each-ref --sort=-creatordate refs/heads/ --format=\"%(creatordate:short) %(refname:short)\"; }; f"
But a better version would be:
bls = ! "git for-each-ref --sort=-creatordate refs/heads refs/remotes --format='%(creatordate:short) %(objectname:short) %(align:40)%(refname:short)%(end) %(contents:subject)'"
git config --local alias.foo '!f() { git version && echo foo; }; f'- otherwise replace your single quotes with double quotesbls = '!f() { git branch -vv --list --all && echo \"\nSorted by creation date:\" && git for-each-ref --sort=-creatordate refs/heads/ -- format=\"%(creatordate:short) %(refname:short)\"; }; f'tobls = "!f() { git branch -vv --list --all && echo \"\nSorted by creation date:\" && git for-each-ref --sort=-creatordate refs/heads/ --format=\"%(creatordate:short) %(refname:short)\"; }; f", I still receive an error:expansion of alias 'bls' failed; 'git' is not a git command