gitshellterminalzsh

Git alias for pulling all remote branches


I'm trying to make a git alias to pull remote branches based on this answer

git branch -r \
  | grep -v '\->' \
  | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" \
  | while read remote; do \
      git branch --track "${remote#origin/}" "$remote"; \
    done
git fetch --all
git pull --all

I'm trying to make an alias for this:

git config --global alias.pull-all '!'"f() { git branch -r | grep -v -- '->' | sed \"s,\x1B\[[0-9;]*[a-zA-Z],,g\" | while read b; do git branch --track \"\${b##origin/}\" \"\$b\"; done; git pull --all }; f" 

But when I use the alias I get a shell (zsh) syntax error "unexpected end of file":

$ git pull-all
f() { git branch -r | grep -v -- '->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read b; do git branch --track "${b##origin/}" "$b"; done; git pull --all }; f: -c: line 1: syntax error: unexpected end of file

But copy-pasting the error message into my command line works perfectly:

$ f() { git branch -r | grep -v -- '->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read b; do git branch --track "${b##origin/}" "$b"; done; git pull --all }; f
branch 'a-branch' set up to track 'origin/a-branch'.
fatal: a branch named 'main' already exists
Already up to date.

How can I fix the alias so that all remote branches will be pulled, just as they are when I enter the command manually?


Solution

  • Turns out functions need a semicolon or newline before the closing brace.

    git config --global alias.pull-all '!'"f() { git branch -r | grep -v -- '->' | sed \"s,\x1B\[[0-9;]*[a-zA-Z],,g\" | while read b; do git branch --track \"\${b##origin/}\" \"\$b\"; done; git pull --all; }; f"