Adding Git Information to Your zsh Prompt (PS1)

Adding Git Information to Your zsh Prompt (PS1)

Logo
The Bytestack TeamPublished 2024-07-17

Customizing your zsh prompt (PS1) to display Git information is a great way to enhance your command line experience, providing immediate context about the status of your repositories. In this blog, we will walk through adding Git status information to your zsh prompt, including clean/sync status indicators and color customization.

Step 1: Install zsh and git

Before we start, ensure you have zsh and git installed on your system.

# Install zsh sudo apt install zsh # Install git sudo apt install git

Step 2: Install oh-my-zsh

oh-my-zsh is a popular framework for managing your zsh configuration. It includes a variety of plugins and themes that can simplify the process of adding Git information to your prompt.

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Step 3: Choose and Configure a Theme

Many oh-my-zsh themes come with Git information built-in. One such theme is agnoster. To use it, open your .zshrc file and set the ZSH_THEME variable:

# Open .zshrc in your preferred editor nano ~/.zshrc # Set the theme ZSH_THEME="agnoster"

After editing, reload your zsh configuration:

source ~/.zshrc

Step 4: Customize the Prompt for Git Status

If you prefer a custom prompt or need specific Git information not covered by a pre-made theme, you can manually configure your PS1. First, create a function to fetch Git status information.

Open your .zshrc file and add the following:

# Function to get the current Git branch function git_branch() { git symbolic-ref --short HEAD 2>/dev/null } # Function to get the Git status function git_status() { git status --porcelain 2>/dev/null | grep -E "^\s?[MADRCU]|\?\?" &> /dev/null if [[ $? -eq 0 ]]; then echo "%F{red}✗%f" # Uncommitted changes else echo "%F{green}✓%f" # Clean working directory fi } # Function to get the remote status function git_remote_status() { local branch=$(git_branch) if [ -n "$branch" ]; then local local_hash=$(git rev-parse @) local remote_hash=$(git rev-parse @{u} 2>/dev/null) local base_hash=$(git merge-base @ @{u} 2>/dev/null) if [ "$local_hash" = "$remote_hash" ]; then echo "%F{yellow}⇡⇣%f" # Up to date elif [ "$local_hash" = "$base_hash" ]; then echo "%F{blue}⇣%f" # Need to pull elif [ "$remote_hash" = "$base_hash" ]; then echo "%F{magenta}⇡%f" # Need to push else echo "%F{red}⚡%f" # Diverged fi fi }

Next, customize your PS1 to include these functions. Add this to your .zshrc file:

# Custom PS1 with Git information export PS1='%F{cyan}%n@%m%f %F{yellow}%~%f $(git_branch)$(git_status)$(git_remote_status) %# '

This configuration sets your prompt to display:

  • Your username and host (%n@%m)
  • The current working directory (%~)
  • The current Git branch ($(git_branch))
  • The Git status ($(git_status))
  • The remote sync status ($(git_remote_status))

Step 5: Reload the Configuration

To apply your changes, reload your zsh configuration:

source ~/.zshrc

Conclusion

By following these steps, you now have a zsh prompt that shows valuable Git information, including the branch name, working directory status, and remote sync status, all color-coded for quick recognition. This setup can significantly boost your productivity by keeping you informed about your Git repository status directly from your terminal prompt. Happy coding!

Ready to dive in?Start your free trial today.

Experience the power of Bystack for your team.