To copy standard input to standard output, use cat, not echo.
git ls-remote "$1" 'refs/heads/*' |
sed 's~.*/~~' |
if [ -z "$2" ]
  then
    cat
  else
    cat > "$2"
fi
Notice also the proper use of quotes and the placement of the pipes so you can avoid the backslashes. The use of sed is a very minor optimization but I also find it clearer than the double rev around a cut (provided you grok regex). You could also use awk -F/ '{ print $NF }' (but then that requires you to grok Awk).
You could avoid the cat by doing this instead;
${2+exec >"$2"}
git ls-remote "$1" | sed 's~.*/~~'
(The failure if you pass in an empty string as the second argument should at least be more explicit, if not necessarily more helpful, than with [ -z, which fails to distinguish between an unset and an empty value.)
     
    
cat, notecho."$1"and"$2".ifstatement, ... Sorry, I can't think of any simpler way to say it. I think you wantcat.rev | cut | revwithsed 's~.*/~~'