Table of Contents#
- Prerequisites
- Understanding the Linux Shell Prompt
- Step 1: Identify Your Shell
- Step 2: Create a Git Branch Detection Function
- Step 3: Modify the Prompt to Show the Git Branch
- Step 4: Add Color to the Git Branch (and Prompt)
- Step 5: Test Your New Prompt
- Troubleshooting Common Issues
- Adapting for Zsh (Optional)
- Conclusion
- References
Prerequisites#
Before starting, ensure you have the following:
- Git installed: Run
git --versionin the terminal. If not installed, use your package manager (e.g.,sudo apt install gitfor Debian/Ubuntu,sudo dnf install gitfor Fedora, orsudo pacman -S gitfor Arch). - A Linux terminal: Most default terminals (GNOME Terminal, Konsole, Tilix, etc.) work. Ensure it supports ANSI color codes (nearly all modern terminals do).
- Basic shell knowledge: Familiarity with editing text files (e.g., using
nano,vim, orgedit) and running terminal commands.
Understanding the Linux Shell Prompt#
The terminal prompt you see (e.g., user@hostname:~/project$ ) is controlled by a shell variable called PS1 (Prompt String 1). This variable defines the layout and content of the primary prompt.
To see your current PS1 value, run:
echo $PS1Default PS1 values vary by shell and distribution but often include placeholders like:
\u: Current username\h: Hostname (shortened)\w: Current working directory (full path)\$: Displays$for regular users or#for root
Our goal is to modify PS1 to include the current Git branch (when in a Git repository) and add color.
Step 1: Identify Your Shell#
Most Linux systems use Bash (Bourne Again Shell) by default, but some use Zsh (Z Shell). To check your shell, run:
echo $SHELL- If the output is
/bin/bash, follow the Bash instructions below. - If it’s
/bin/zsh, skip to the Zsh adaptation section after the Bash steps.
Step 2: Create a Git Branch Detection Function#
To display the Git branch, we need a way to detect it. The git command provides this via:
git rev-parse --abbrev-ref HEADThis returns the current branch name (e.g., main, feature/new-ui) if you’re in a Git repository. If not, it outputs nothing.
We’ll wrap this in a shell function to make it reusable. Open your shell configuration file (for Bash, this is ~/.bashrc; for Zsh, ~/.zshrc) using a text editor like nano:
nano ~/.bashrcScroll to the bottom of the file and add the following function:
# Function to get current Git branch (if in a Git repo)
git_branch() {
# Use git rev-parse to get branch; suppress errors (e.g., if not a Git repo)
local branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)
# If branch exists, return it in parentheses; otherwise, return empty
if [ -n "$branch" ]; then
echo " ($branch)"
fi
}Save the file (in nano, press Ctrl+O, then Enter, then Ctrl+X to exit).
Step 3: Modify the Prompt to Show the Git Branch#
Now, update PS1 to include the git_branch function’s output. In ~/.bashrc, find the existing PS1 line (it may look like PS1='\u@\h:\w\$ '). If there’s no PS1 line, add a new one.
Modify PS1 to include $(git_branch) where you want the branch to appear. For example, to show the branch after the working directory:
# Old PS1 (example)
# PS1='\u@\h:\w\$ '
# New PS1 with Git branch
PS1='\u@\h:\w$(git_branch)\$ 'Explanation:
\u@\h: Username and hostname (e.g.,alice@my-laptop).:\w: Current working directory (e.g.,:~/projects/my-app).$(git_branch): Inserts the branch name (e.g.,(main)).\$: The$prompt symbol.
Step 4: Add Color to the Git Branch (and Prompt)#
To make the branch stand out, add ANSI color codes to PS1. ANSI codes let you set text color, background color, and styles (bold, underline).
Common ANSI Color Codes#
| Code | Color/Style |
|---|---|
\033[0m | Reset (default) |
\033[0;32m | Green (regular) |
\033[1;32m | Green (bold) |
\033[0;31m | Red (regular) |
\033[1;34m | Blue (bold) |
\033[1;36m | Cyan (bold) |
Formatting the Prompt with Color#
Wrap the Git branch (and other prompt elements) in color codes. For example:
# Colored PS1: Username (blue), Hostname (cyan), Directory (green), Branch (purple)
PS1='\[\033[1;34m\]\u@\h\[\033[0m\]:\[\033[1;32m\]\w\[\033[0m\]\[\033[1;35m\]$(git_branch)\[\033[0m\]\$ 'Breakdown:
\[\033[1;34m\]: Start bold blue for\u@\h.\[\033[0m\]: Reset to default (ends blue).:\[\033[1;32m\]\w: Green bold for the working directory.\[\033[1;35m\]$(git_branch): Purple bold for the Git branch.\[\033[0m\]: Reset to default before the$symbol.
Note: The
\[ \]brackets tell Bash that the ANSI codes are non-printable characters, preventing line-wrapping glitches.
Step 5: Test Your New Prompt#
To apply the changes, reload your shell configuration:
source ~/.bashrcNow, open a new terminal or navigate to a Git repository (e.g., cd ~/projects/your-git-repo). You should see:
alice@my-laptop:~/projects/your-git-repo (main)$
- The branch name
(main)will appear in purple (or your chosen color). - If you navigate out of the Git repo, the branch disappears:
alice@my-laptop:~/Downloads$
Troubleshooting Common Issues#
1. Git Branch Not Showing Up#
- Issue: The branch isn’t displayed even in a Git repo.
- Fix:
- Ensure Git is installed:
git --version. - Verify the
git_branchfunction is defined in~/.bashrc(no typos!). - Run
git rev-parse --abbrev-ref HEADin the repo to test: it should return the branch name.
- Ensure Git is installed:
2. No Color in the Prompt#
- Issue: The prompt has the branch but no color.
- Fix:
- Ensure your terminal supports ANSI colors (most modern terminals do; try GNOME Terminal or Konsole if unsure).
- Check for typos in ANSI codes (e.g.,
\033instead of\033). - Verify
\[ \]brackets wrap non-printable codes (missing brackets cause color glitches).
3. Prompt Glitches (Line Wrapping)#
- Issue: When typing long commands, the text overlaps itself.
- Fix: Ensure all ANSI color codes are wrapped in
\[ \](e.g.,\[\033[1;34m\]instead of\033[1;34m).
Adapting for Zsh (Optional)#
If you use Zsh, the process is similar but uses ~/.zshrc instead of ~/.bashrc. Here’s the adapted setup:
-
Open
~/.zshrc:nano ~/.zshrc -
Add the
git_branchfunction:git_branch() { local branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null) if [ -n "$branch" ]; then echo " ($branch)" fi } -
Modify
PROMPT(Zsh usesPROMPTinstead ofPS1):PROMPT='%B%F{blue}%n@%m%f%b:%B%F{green}%~%f%b%B%F{purple}$(git_branch)%f%b $ '%B: Bold on,%b: Bold off.%F{blue}: Blue text,%f: Reset color.%n@%m: Username@hostname,%~: Current directory (shorthand).
-
Reload Zsh:
source ~/.zshrc
Conclusion#
You’ve now configured your Linux terminal to display the current Git branch with color, just like Git Bash on Windows! This setup saves time, reduces errors, and makes your terminal more informative.
For advanced users, explore:
- Adding a "dirty" indicator (e.g.,
*if there are uncommitted changes) usinggit diff --quietin thegit_branchfunction. - Customizing colors based on branch state (e.g., red for
main, green for feature branches).