Customizing the Bash prompt to display Git information is a powerful way to improve your command-line workflow. By integrating Git branch names, statuses, and other repository details into the Bash prompt, you can streamline your development process and always be aware of the state of your repository. This article explains how to customize Git prompts in Bash, with examples and best practices.
TL;DR
To add Git information to your Bash prompt:
1. Source the git-prompt.sh
script in your .bashrc
file.
source /usr/share/git-core/contrib/completion/git-prompt.sh
2. Customize the PS1
variable to include the __git_ps1
function:
PS1='[\u@\h \w$(__git_ps1 " (%s)")]\$ '
3. Reload your Bash configuration:
source ~/.bashrc
Download the commands and script
Git Prompts in Bash
A Git-enabled Bash prompt typically displays the current branch name and its status. This is achieved using the git-prompt.sh
script, which provides the __git_ps1
function. This function dynamically updates the prompt based on the repository state.
Example:
user@hostname ~/project (main)$
Here:
main
is the current branch.- The prompt updates automatically when you switch branches or make changes.
Step-by-Step: Adding Git Information to the Bash Prompt
1. Ensure Git is Installed
Verify Git is installed on your system:
git --version
If not installed, follow the Git installation guide.
2. Source the git-prompt.sh
Script
The git-prompt.sh
script is typically included with Git installations. It provides the __git_ps1
function for displaying Git status in the prompt.
Locate the Script: Common locations include:
/usr/share/git-core/contrib/completion/git-prompt.sh
/usr/share/bash-completion/completions/git-prompt.sh
Source the Script: Add the following line to your ~/.bashrc
file:
source /usr/share/git-core/contrib/completion/git-prompt.sh
If the script is not available, you can download it from GitHub.
3. Customize the PS1
Variable
Modify the PS1
variable in your ~/.bashrc
file to include the __git_ps1
function.
Basic Prompt with Git Branch:
PS1='[\u@\h \w$(__git_ps1 " (%s)")]\$ '
Explanation:
\u
: Displays the username.\h
: Displays the hostname.\w
: Displays the current working directory.$(__git_ps1 " (%s)")
: Adds the Git branch name in parentheses.\$
: Displays#
for root and$
for non-root users.
Example Output:
user@hostname ~/project (main)$
4. Add Git Status Indicators
To include additional information like untracked changes or staged files, use the __git_ps1
function with options.
Prompt with Status Indicators:
PS1='[\u@\h \w$(__git_ps1 " (%s)" "%s")] \$ '
Explanation:
%s
: Adds indicators for repository status, such as:+
: Staged changes.*
: Unstaged changes.?
: Untracked files.
Example Output:
user@hostname ~/project (main*)$
This indicates that the main
branch has unstaged changes.
Testing and Debugging
After modifying the ~/.bashrc
file, reload the configuration:
source ~/.bashrc
If the prompt doesn’t update:
- Verify the path to
git-prompt.sh
is correct. - Ensure the
PS1
syntax is free of typos. - Test the
__git_ps1
function directly:
__git_ps1
Advanced Customization
Add Colors to the Prompt
You can add colors for better visibility using ANSI escape codes.
Example:
PS1='\[\033[1;34m\]\u@\h\[\033[0m\] \[\033[1;32m\]\w\[\033[0m\]$(__git_ps1 " \[\033[1;31m\](%s)\[\033[0m\]") \$ '
\033[1;34m
: Blue for username and hostname.\033[1;32m
: Green for the working directory.\033[1;31m
: Red for the Git branch.
Include Virtual Environment Information
If you work with Python virtual environments, you can prepend the environment name to the prompt:
PS1='(\$(basename \$(dirname \$(readlink -f \$(which python))))) [\u@\h \w$(__git_ps1 " (%s)")]\$ '
Reference Links
- Customize Git Prompts in Bash
- Git Prompt Documentation
- GNU Bash Manual
- GitHub: git-prompt.sh
- ArchLinux: Bash/Prompt customization