Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ethaizone/232664a147258fb6a5b91ba2bf790f37 to your computer and use it in GitHub Desktop.
Save ethaizone/232664a147258fb6a5b91ba2bf790f37 to your computer and use it in GitHub Desktop.
Add Git Branch Name and Status to Terminal Prompt (MacOS zsh)

Add Git Branch Name and Status to Terminal Prompt (zsh)

image

Install

Open ~/.zshrc in your favorite editor and add the following content to the bottom.

git_status() {
    # Early return if not in a git repository
    git rev-parse --is-inside-work-tree &>/dev/null || return

    local status_symbols=("")
    local git_status="$(git status --porcelain -b 2>/dev/null)"
    local working_tree=$(echo "$git_status" | tail -n +2)  # Skip the first line (branch info)

    # Branch status with colors
    if echo "$git_status" | grep -q "ahead"; then
        status_symbols+=("%F{cyan}↑%f")    # Cyan for ahead
    elif echo "$git_status" | grep -q "behind"; then
        status_symbols+=("%F{yellow}↓%f")  # Yellow for behind
    elif echo "$git_status" | grep -q "diverged"; then
        status_symbols+=("%F{red}↕%f")     # Red for diverged
    fi

    # Working tree status with colors
    if [[ -z "$working_tree" ]]; then
        status_symbols+=("%F{green}✔%f")   # Green for clean
    else
        # Check for untracked files
        if echo "$git_status" | grep -q "^??"; then
            status_symbols+=("%F{blue}?%f")    # Blue for untracked
        fi

        # Check for unstaged modifications
        if echo "$git_status" | grep -q "^.M"; then
            status_symbols+=("%F{yellow}!%f")  # Yellow for modified
        fi

        # Check for unstaged deletions
        if echo "$git_status" | grep -q "^.D"; then
            status_symbols+=("%F{red}-%f")     # Red for deleted
        fi

        # Check for staged additions
        if echo "$git_status" | grep -q "^A"; then
            status_symbols+=("%F{magenta}+%f") # Magenta for added
        fi

        # Check for staged modifications
        if echo "$git_status" | grep -q "^M"; then
            status_symbols+=("%F{magenta}M%f") # Magenta for staged modifications
        fi

        # Check for staged deletions
        if echo "$git_status" | grep -q "^D"; then
            status_symbols+=("%F{magenta}D%f") # Magenta for staged deletions
        fi

        # Check for renamed files
        if echo "$git_status" | grep -q "^R"; then
            status_symbols+=("%F{cyan}R%f")    # Cyan for renamed
        fi

        # Check for copied files
        if echo "$git_status" | grep -q "^C"; then
            status_symbols+=("%F{cyan}C%f")    # Cyan for copied
        fi

        # Check for merge conflicts
        if echo "$git_status" | grep -q "^U\|^.U\|^AA\|^DD"; then
            status_symbols+=("%F{red}≠%f")     # Red for conflicts
        fi
    fi

    echo "${(j::)status_symbols}"
}

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

COLOR_DEF=$'%f'
COLOR_USR=$'%F{243}'
COLOR_DIR=$'%F{197}'
COLOR_GIT=$'%F{39}'
NEWLINE=$'\n'
PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}$(git_status)${NEWLINE}$ '

Reload and apply adjustments

To reload and apply adjustments to the formatting use source ~/.zshrc in the zsh shell.

Credits

@ethaizone
Copy link
Author

ethaizone commented Feb 20, 2025

By the way, I forked and use this one since I previously used https://github.com/romkatv/powerlevel10k. However, I found an issue where VSCode shell integration breaks when using this theme, so I removed it and now use this basic prompt. Now I can properly use GitHub Copilot or Cline to run any commands.

Here is issue for reference: romkatv/powerlevel10k#1827

@ethaizone
Copy link
Author

ethaizone commented Feb 20, 2025

Update:

  • Just improve git_status for more cases. Suggestion from Claude. I don't test for every cases so let me know if any cases isn't working.
  • Improve by call git status for one time only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment