git oh-my-zsh

Speeding Up Zsh Prompt in Large Git Repositories

Marcelino Veloso III,

When using oh-my-zsh with large Git repositories, the shell prompt can become slow because the default git_prompt_info runs expensive Git status checks.

This snippet, originally found in a comment from Github, provides a lighter replacement for git_prompt_info that avoids unnecessary commands and speeds up prompt rendering in large repos.

Instructions

  1. Save the following function override before sourcing oh-my-zsh.sh in .zshrc.
  2. Run git config inside any large repository to configure the prompt behavior (optional, see below).

Function Override

plugins=(
  git
  zsh-autosuggestions
  zsh-syntax-highlighting
)

# Faster Git prompt for oh-my-zsh on large repositories
# Source: https://gist.github.com/msabramo/2355834

function git_prompt_info() {
  local ref
  # Only show Git info if dirty check is not hidden
  if [[ "$(command git config --get oh-my-zsh.hide-dirty)" != "1" ]]; then
    # Only show branch if status is not hidden
    if [[ "$(__git_prompt_git config --get oh-my-zsh.hide-status 2>/dev/null)" != "1" ]]; then
      # Try to get branch name, otherwise fallback to commit hash
      ref=$(__git_prompt_git symbolic-ref HEAD 2> /dev/null) || \
      ref=$(__git_prompt_git rev-parse --short HEAD 2> /dev/null) || return 0

      # Print branch/commit with theme-defined prefix/suffix
      echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX"
    fi
  fi
}

# Important: Must be placed BEFORE loading oh-my-zsh
source $ZSH/oh-my-zsh.sh

Notes

  • __git_prompt_git is a helper function from oh-my-zsh for running Git commands safely.
  • parse_git_dirty checks if there are uncommitted changes (disabled if oh-my-zsh.hide-dirty is set).
  • This override is only necessary in very large repositories where prompt lag is noticeable.