User Agents

Web browsers and everyday web tech, explained

Zsh Tab Completion for Git Without Origin

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Zsh's tab completion for Git normally suggests remote names like origin when you type git push or git pull. If you work with repositories that have no origin remote (or you want to push to a different remote), the default behavior can be annoying—it may show nothing or only suggest the current branch. This guide shows you how to configure zsh so that tab completion works smoothly for Git commands even when origin is missing. You'll learn to make completion suggest local branches, other remotes, and even set up a fallback that avoids errors.

What You'll Need

  • Zsh installed (check with zsh --version).
  • Git installed (check with git --version).
  • A basic understanding of your shell configuration file (~/.zshrc).
  • Optional: a Git repository without an origin remote to test with.

Step-by-Step

  1. Check your current completion setup. Run echo $fpath and see if compinit is loaded. If you use a framework like Oh My Zsh, it's usually already enabled. If not, add autoload -Uz compinit && compinit to your ~/.zshrc and restart your shell.
  2. Enable Git completion. If you use Oh My Zsh, ensure git is in the plugins=(...) list. For plain zsh, the Git completion script is usually included with zsh; if not, install the zsh-completions package and add its directory to your fpath before compinit.
  3. Understand the default behavior. In a repo with no origin, typing git push may show nothing or only the current branch. This happens because zsh's Git completion tries to resolve the remote name from origin first.
  4. Use the __git_remotes function. Zsh's Git completion defines a helper function that lists all remotes. You can force it to suggest all remotes by typing git push after a space—if you type git push , it should list all remotes. If it doesn't, you may need to update your completion files.
  5. Set a fallback remote. If you always want to push to a specific remote (e.g., upstream), you can set the Git config: git config push.default upstream and git config branch..remote upstream. This doesn't affect completion directly but makes git push work without typing a remote.
  6. Customize the completion behavior. Add this to your ~/.zshrc to make completion suggest local branches when no remote is configured:
    # Fallback to local branches for git push/pull
    zstyle ':completion:*:*:git:*' user-commands ${${(M)${(k)commands}:#git}:-} ${${(M)${(k)functions}:#_git-*}:-}
    zstyle ':completion:*:*:git:*' command 'git --no-pager'
    zstyle ':completion:*:*:git:*' ignored-patterns 'origin'
    
    The last line tells zsh to ignore origin in completion suggestions. This is useful if you have multiple remotes and want to avoid accidentally pushing to origin.
  7. Test your setup. Navigate to a Git repo without origin and type git push . You should see a list of local branches (if any) and other remotes. If you have no other remotes, you might see nothing—that's expected.
  8. Alternative: use a function to list remotes. If you prefer a more explicit approach, create a custom completion function. Add this to your ~/.zshrc:
    # Custom completion for git push/pull without origin
    _git_push() {
      _arguments '1: :__git_remotes' '*: :__git_branch_names'
    }
    _git_pull() {
      _arguments '1: :__git_remotes' '*: :__git_branch_names'
    }
    
    This overrides the default completions for git push and git pull to always list remotes and branch names.
  9. Reload your shell. Run source ~/.zshrc or restart your terminal to apply changes.

Common Mistakes

  • Forgetting to run compinit after changing fpath. If you add a new completion directory, you must re-initialize completions.
  • Typing git push without a space before TAB. In zsh, git push completes the command name, not arguments. Always include a space.
  • Assuming origin is always present. Many tutorials assume a standard remote; if you cloned from a fork, your remote might be upstream or something else.
  • Overriding Git's built-in completion incorrectly. If you define _git_push in your .zshrc, it may conflict with the official completion. Use the function only if you know what you're doing.

Tips

  • Use git remote -v to see all remotes before relying on completion.
  • Set push.default to current if you often push to a branch with the same name on a remote.
  • If you work with multiple remotes, consider naming them consistently (e.g., upstream, fork) to make completion clearer.
  • Use git config --global --list to check your global Git settings that might affect completion.
  • For a quick test, run git push --dry-run to see what zsh suggests without actually pushing.

Troubleshooting

  • Completion shows nothing at all. First, check if Git completion is loaded: run which _git. If it's not found, reinstall zsh-completions and ensure fpath includes the directory. Then run compinit again.
  • Completion shows only the current branch. This happens when zsh thinks the remote is the current branch's upstream. Run git branch -vv to see upstream info. If the branch has no upstream, zsh may fall back to local branches—which is what you want.
  • Completion suggests origin even though it doesn't exist. This can happen if your Git config has a stale remote. Run git remote remove origin if it's not used, or check git config --get-regexp 'remote\..*\.url'.
  • After adding custom functions, completion breaks. Remove the custom functions from .zshrc and restart. The official Git completion is usually more robust.
  • Completion works in bash but not zsh. Zsh uses a different completion system. Make sure you're using zsh's native completion, not bash's.

Frequently Asked Questions

Why does zsh not complete remote names when there's no origin?

Zsh's Git completion is designed to suggest remotes that are configured. If no remote is set, or if the remote is not named origin, it may not show anything because it doesn't know what to suggest. The steps above help it list all remotes or local branches.

Can I make tab completion always ask for a remote?

Yes, by overriding the _git_push function as shown in Step 8, you can force zsh to always list remotes. However, this may be redundant if you have only one remote.

Does this work for git pull and git fetch too?

Yes, the same principles apply. You can extend the custom function to cover git fetch and git merge if needed.

What if I have multiple remotes and want to exclude one?

Use the ignored-patterns zstyle as shown in Step 6 to exclude specific names like origin from suggestions.

Is there a way to see all remotes without typing them?

Yes, just type git remote -v to list all remotes. For completion, you can also type git push after configuring as above.

Will this affect my existing Git workflows?

No, it only changes how tab completion behaves. Your actual Git commands remain unchanged. If you have scripts relying on origin, they will still work as long as the remote exists.