Getting error when creating an alias which includes sed -e

Getting error when creating an alias which includes sed -e
0
#1

So I found this handy git command on https://davidwalsh.name/sort-git-branches

git for-each-ref --sort='-committerdate' --format='%(refname)%09%(committerdate)' refs/heads | sed -e 's-refs/heads/--'

It works fine if I use it directly on the command line (I am on Windows 10 using Git Bash):

[email protected] MINGW64 /d/coding/fcc (master)
$ git for-each-ref --sort='-committerdate' --format='%(refname)%09%(committerdate)' refs/heads | sed -e 's-refs/heads/--'
master  Mon Feb 25 16:37:58 2019 +0300
fix/correct-404-when-clicking-on-get-a-hint-for-use-the-env-file-challenge      Mon Feb 25 01:56:23 2019 -0800
fix/correct-wrong-checks-in-test        Sun Feb 24 21:40:27 2019 -0800
fix/stand-in-line-desc-to-remove-confusing-queue-code-tags      Sun Feb 24 19:56:23 2019 -0800
fix/update-challenge-template-in-doc    Sun Feb 24 14:49:09 2019 -0800
fix/add-test-to-validate-only-clockwise-notation-used-for-margin        Sun Feb 24 10:10:26 2019 -0800
feat/use-minifier-to-strip-out-whitespace-from-challenge-code-string    Sat Feb 23 11:54:42 2019 -0800
fix/prevent-invalid-solution-from-passing-tests Fri Feb 22 14:43:07 2019 -0800
fix/test-fails-with-comments    Thu Feb 21 13:07:47 2019 -0800
fix/allow-using-actual-space-characters-in-regex-expression     Thu Feb 21 00:23:45 2019 -0800
fix/use-correct-variable-destructing-assignment-nested-objects  Wed Feb 20 21:43:23 2019 -0800
fix/allow-same-line-comments-in-jsx     Wed Feb 20 19:25:12 2019 -0800
fix/example     Wed Feb 13 17:55:30 2019 -0800
fix/corrected-typo      Sun Feb 10 10:46:40 2019 -0800
fix/allow-spaces-when-incrementing-index        Thu Feb 7 22:49:35 2019 -0800
fix/bug-allowing-version-of-object-keys Thu Jan 31 20:14:37 2019 -0800
fix/remove-add-your-username-and-wkiki-challenge-solution-template-references-from-spanish-guide        Thu Jan 24 10:16:18 2019 -0800
fix/remove-add-your-username-and-wkiki-challenge-solution-template-references-from-russian-guide        Thu Jan 24 10:09:13 2019 -0800
fix/remove-add-your-username-and-wkiki-challenge-solution-template-references-from-portuguese-guide     Thu Jan 24 09:59:47 2019 -0800
fix/remove-add-your-username-and-wkiki-challenge-solution-template-references-from-chinese--guide       Sun Jan 20 14:38:41 2019 -0800
fix/remove-add-your-username-and-wkiki-challenge-solution-template-references-from-arabic-guide Sun Jan 20 13:57:38 2019 -0800
fix/remove-add-your-username-and-wkiki-challenge-solution-template-references-from-english-guide        Sun Jan 20 13:16:40 2019 -0800
fix/consolidate-php-loop-and-loops-folders      Sat Dec 29 14:19:00 2018 -0800
fix-tests-for-import-all-challenge      Mon Dec 10 19:08:40 2018 -0800

However, if I try to create an alias in my .gitconfig file like the following:

[alias]
	co = checkout
    cob = checkout -b
    branchdate = for-each-ref --sort='-committerdate' --format='%(refname)%09%(committerdate)' refs/heads | sed --expression 's-refs/heads/--'

then I get the following error message:

[email protected] MINGW64 /d/coding/fcc (master)
$ git branchdate
error: unknown switch `e'
usage: git for-each-ref [<options>] [<pattern>]
   or: git for-each-ref [--points-at <object>]
   or: git for-each-ref [(--merged | --no-merged) [<commit>]]
   or: git for-each-ref [--contains [<commit>]] [--no-contains [<commit>]]

    -s, --shell           quote placeholders suitably for shells
    -p, --perl            quote placeholders suitably for perl
    --python              quote placeholders suitably for python
    --tcl                 quote placeholders suitably for Tcl

    --count <n>           show only <n> matched refs
    --format <format>     format to use for the output
    --color[=<when>]      respect format colors
    --sort <key>          field name to sort on
    --points-at <object>  print only refs which points at the given object
    --merged <commit>     print only refs that are merged
    --no-merged <commit>  print only refs that are not merged
    --contains <commit>   print only refs which contain the commit
    --no-contains <commit>
                          print only refs which don't contain the commit
    --ignore-case         sorting and filtering are case insensitive

How can I get this to work as an alias?

#2

I got some advice from someone else (@raisedadead) who suggested I use a function alias instead. Worked like a charm.

bdate = "!f() { git for-each-ref --sort='-committerdate' --format='%(refname)%09%(committerdate)' refs/heads | sed -e 's-refs/heads/--' ;}; f"
1 Like