Skip to content

Instantly share code, notes, and snippets.

@ryanhossain9797
Created October 8, 2025 05:55
Show Gist options
  • Select an option

  • Save ryanhossain9797/fe3ec8d6d0d9972980d0ad8bebdb4c76 to your computer and use it in GitHub Desktop.

Select an option

Save ryanhossain9797/fe3ec8d6d0d9972980d0ad8bebdb4c76 to your computer and use it in GitHub Desktop.
My Custom Prompt
#!/bin/bash
# --- Custom Git Prompt Script ---
# This script is meant to be SOURCED, not executed.
# e.g., append `source prompt.sh` at the end of .bashrc
# This function gets the current git branch and status
parse_git_status() {
# Return early if not in a git repository
local git_dir
git_dir=$(git rev-parse --git-dir 2>/dev/null)
if [ -z "$git_dir" ]; then
return
fi
# --- CONFIGURATION ---
local symbol_clean="✔"
local symbol_dirty="*"
local symbol_ahead="↑"
local symbol_behind="↓"
local symbol_stashed="S"
# --- END CONFIGURATION ---
local branch ahead behind dirty stashed status_string
# Get current branch name
branch=$(git branch --show-current)
# Get ahead/behind counts from remote
ahead=$(git rev-list --left-only --count @{u}...HEAD 2>/dev/null || echo 0)
behind=$(git rev-list --right-only --count @{u}...HEAD 2>/dev/null || echo 0)
# Check for uncommitted changes (a "dirty" state)
if [[ -n $(git status --porcelain) ]]; then
dirty=" $symbol_dirty"
else
dirty=" $symbol_clean"
fi
# Check for stashed changes
if git rev-parse --verify refs/stash &>/dev/null; then
stashed=" $symbol_stashed"
fi
# Build the status string
status_string="($branch"
if [[ "$ahead" -gt 0 ]]; then
status_string+=" $symbol_ahead$ahead"
fi
if [[ "$behind" -gt 0 ]]; then
status_string+=" $symbol_behind$behind"
fi
status_string+="$dirty$stashed)"
echo "$status_string"
}
# --- Prompt Configuration ---
# Define colors for readability
# The \[ and \] symbols are important to prevent line-wrapping issues
COLOR_YELLOW="\[\033[1;33m\]"
COLOR_GREEN="\[\033[0;32m\]"
COLOR_BLUE="\[\033[0;34m\]"
COLOR_RESET="\[\033[0m\]"
# PROMPT_COMMAND is executed just before the shell displays the prompt.
# We use it to dynamically build PS1 each time.
# \n creates the newline for the two-line prompt.
PROMPT_COMMAND='PS1="${COLOR_GREEN}\u@\h${COLOR_RESET}:${COLOR_BLUE}\w${COLOR_RESET} ${COLOR_YELLOW}$(parse_git_status)${COLOR_RESET}\n❯ "'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment