Have you ever accidentally committed directly to main
or master
? It’s a common mistake that can disrupt your git workflow. Here’s a solution that makes it virtually impossible to miss which branch you’re on by adding a visual alert to your zsh prompt when you’re in a primary branch.
Features
This customized prompt provides several useful features:
Visual Branch Alerts:
- Primary branches (
main
/master
) flash in yellow as a warning - Feature branches appear in a calm blue color
- Branch name is always visible in your prompt
- No branch name is displayed when your current directory does not reside within a git repository
- Primary branches (
Two-Line Prompt Structure:
- Line 1: Shows current directory and git branch
- This allows for longer paths and/or longer branch names
- Line 2: Displays time (HH:MM:SS) followed by prompt symbol
Built-in Timestamp:
- Helps track command execution times
- Enables easy backtracking through command history
- Perfect for informal timing of operations
Implementation
Add the following code to your .zshrc
file:
function parse_git_branch() {
# Get current branch name, wrapped in brackets
branch=$(git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p')
# Special handling for main/master branches - make them flash yellow
if [[ "${branch}" == "[main]" ]] ; then
printf '%s%s%s%s' "$(tput setaf 3)" "$(tput blink)" "[main]" "$(tput sgr0)"
elif [[ "${branch}" == "[master]" ]] ; then
printf '%s%s%s%s' "$(tput setaf 3)" "$(tput blink)" "[master]" "$(tput sgr0)"
else
# For all other branches, display without special formatting
echo "${branch}"
fi
}
# Define colors for different prompt elements
COLOR_DEF=$'%f' # Default color
COLOR_DATE=$'%F{243}' # Faded Gray for timestamp
COLOR_DIR=$'%F{197}' # Red for directory
COLOR_GIT=$'%F{39}' # Blue for git branch
# Enable prompt substitution
setopt PROMPT_SUBST
# Build the working directory and git branch portion
PROMPT_CWD='${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}'
# Add newline support
export NEWLINE=$'\n'
# Construct the final prompt
export PROMPT="${NEWLINE}${PROMPT_CWD}${NEWLINE}${COLOR_DATE}%D{%H:%M:%S}${COLOR_DEF} $ "
Code Breakdown
Git Branch Parsing:
- The
parse_git_branch
function captures the current branch name - Uses
2> /dev/null
to suppress errors in non-git directories - Formats branch name with brackets using
sed
- The
Branch Highlighting:
- Uses
tput
commands for terminal formatting:setaf 3
: Set yellow text colorblink
: Enable blinking textsgr0
: Reset all formatting
- Uses
Color Definitions:
- Uses zsh’s color syntax (
%F{number}
) - Separate colors for directory, git info, and timestamp
- Returns to default color after each element
- Uses zsh’s color syntax (
Prompt Structure:
PROMPT_SUBST
: Enables dynamic command substitution%~
: Displays current directory path%D{%H:%M:%S}
: Formats time in HH:MM:SS- Uses
$NEWLINE
for clean two-line layout
Usage
After adding this to your .zshrc
, your prompt will:
- Flash yellow when you’re in
main
ormaster
branches - Show a steady blue branch name for feature branches
- Display the current time with each prompt
- Provide clear visual separation between commands
This setup helps prevent accidental commits to primary branches by making it immediately obvious when you’re in one. The timestamp feature is an added bonus that helps track your development timeline throughout the day.
Remember to source your .zshrc
or restart your terminal for changes to take effect:
source ~/.zshrc