Zsh Tab Completion for Git Without Origin
In this article
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
originremote to test with.
Step-by-Step
- Check your current completion setup. Run
echo $fpathand see ifcompinitis loaded. If you use a framework like Oh My Zsh, it's usually already enabled. If not, addautoload -Uz compinit && compinitto your~/.zshrcand restart your shell. - Enable Git completion. If you use Oh My Zsh, ensure
gitis in theplugins=(...)list. For plain zsh, the Git completion script is usually included with zsh; if not, install thezsh-completionspackage and add its directory to yourfpathbeforecompinit. - Understand the default behavior. In a repo with no
origin, typinggit pushmay show nothing or only the current branch. This happens because zsh's Git completion tries to resolve the remote name fromoriginfirst. - Use the
__git_remotesfunction. Zsh's Git completion defines a helper function that lists all remotes. You can force it to suggest all remotes by typinggit pushafter a space—if you typegit push, it should list all remotes. If it doesn't, you may need to update your completion files. - 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 upstreamandgit config branch.. This doesn't affect completion directly but makes.remote upstream git pushwork without typing a remote. - Customize the completion behavior. Add this to your
~/.zshrcto make completion suggest local branches when no remote is configured:
The last line tells zsh to ignore# 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'originin completion suggestions. This is useful if you have multiple remotes and want to avoid accidentally pushing toorigin. - Test your setup. Navigate to a Git repo without
originand typegit 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. - Alternative: use a function to list remotes. If you prefer a more explicit approach, create a custom completion function. Add this to your
~/.zshrc:
This overrides the default completions for# 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' }git pushandgit pullto always list remotes and branch names. - Reload your shell. Run
source ~/.zshrcor restart your terminal to apply changes.
Common Mistakes
- Forgetting to run
compinitafter changingfpath. If you add a new completion directory, you must re-initialize completions. - Typing
git pushwithout a space beforeTAB. In zsh,git pushcompletes the command name, not arguments. Always include a space. - Assuming
originis always present. Many tutorials assume a standard remote; if you cloned from a fork, your remote might beupstreamor something else. - Overriding Git's built-in completion incorrectly. If you define
_git_pushin your.zshrc, it may conflict with the official completion. Use the function only if you know what you're doing.
Tips
- Use
git remote -vto see all remotes before relying on completion. - Set
push.defaulttocurrentif 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 --listto check your global Git settings that might affect completion. - For a quick test, run
git push --dry-runto 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 ensurefpathincludes the directory. Then runcompinitagain. - Completion shows only the current branch. This happens when zsh thinks the remote is the current branch's upstream. Run
git branch -vvto see upstream info. If the branch has no upstream, zsh may fall back to local branches—which is what you want. - Completion suggests
origineven though it doesn't exist. This can happen if your Git config has a stale remote. Rungit remote remove originif it's not used, or checkgit config --get-regexp 'remote\..*\.url'. - After adding custom functions, completion breaks. Remove the custom functions from
.zshrcand 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.