title: Why is this ZSH function not setting the variables, and outputting a git command instead?
body: <p>Trying to create a custom <code>git</code> function to check to see how far ahead/behind a branch is to another branch. The logic works, if I copy/paste line by line in a terminal it works.</p>
  
  <p>However, putting this in my <code>.zshrc</code> does not:</p>
  
  <pre><code>function gba() {
    branch="$(git rev-parse --abbrev-ref HEAD)"
    counts_str="$(git rev-list --left-right --count origin/dev...$branch)"
    counts=(${(ps:\t:)${counts_str}})
    behind=$counts[1]
    ahead=$counts[2]
  
    echo "$branch behind $behind commits, ahead $ahead commits"
  }
  </code></pre>
  
  <p>Also, if I run in in a script, adding <code>#!/bin/zsh</code> and executing with <code>zsh gba.sh</code> it works.</p>
  
  <p>When I have it in my <code>.zshrc</code> (and make sure I open a new terminal window) it outputs all the <code>git</code> remote branches.</p>
  
  <p>Any ideas? I'd like to have it in my <code>.zshrc</code> so I don't have a separate script file, and would like to run it with just <code>gba</code> or similar like an alias.</p>
  
  <h2>Findings</h2>
  
  <p>Based on suggestions, I've run a few things to see how <code>gba</code> is interpreted when defined directly in my <code>.zshrc</code>:</p>
  
  <pre><code>15:33:41 ~$ which gba
  gba: aliased to git branch -a
  15:33:46 ~$ declare -f gba
  gba () {
    branch="$(git rev-parse --abbrev-ref HEAD)"
    counts_str="$(git rev-list --left-right --count origin/dev...$branch)"
    counts=(${(ps:\t:)${counts_str}})
    behind=$counts[1]
    ahead=$counts[2]
    echo "$branch behind $behind commits, ahead $ahead commits"
  }
  15:34:04 ~$ (set -x; gba)
  +-zsh:3&gt; git branch -a
  fatal: Not a git repository (or any of the parent directories): .git
  </code></pre>
  
  <h2>Solution</h2>
  
  <p>Turns out I didn't realize I had the <code>git</code> <code>oh-my-zsh</code> plugin enabled in <code>.zshrc</code> from the line:</p>
  
  <pre><code>plugins=(git)
  </code></pre>
  
  <p>This loads in a ton of aliases (which I was aware of, but I thought I had it disabled.) So commenting that line makes my <code>gba</code> function work perfectly. The <code>oh-my-zsh</code> <code>git</code> aliases are nice, but I would rather learn the full commands for things as I go, then later manually add aliases if I want to.</p>