iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
👋

Displaying Current Git Branch in Bash Prompt

に公開

Introduction

When working in the terminal, you might often find yourself checking which branch you are currently on.
I have summarized and will introduce a method to display the current branch name and working status in your prompt.

How to Set Up

1. Download git-prompt.sh

Git provides a script for customizing the prompt.
Run the following command in the directory where your .bashrc is located to download the script.

wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh

2. Add settings to .bashrc

Add the following content to your ~/.bashrc.

source ~/.git-prompt.sh

PROMPT_COMMAND='PS1="\[\033[1;32m\]\u\[\033[00m\]:\[\033[1;34m\]\w\[\033[1;31m\]$(__git_ps1)\[\033[00m\] \$ "'

3. Apply changes

To apply the changes, run the following command.

source ~/.bashrc

With this setting, it will be displayed as follows:

user@host:~/repo (main) $

Next, I will explain each element of PS1 in detail.


🔧 About PS1 Settings

PS1 is a variable that defines how the prompt is formatted and displayed.

🏷️ Meaning of Each Element

Code Meaning Displayed Content
\u Username user
\w Current Directory ~/myproject
$(__git_ps1) Git Branch Information (main), (feature-branch), etc.
\$ Indicates User Privileges Normal user: $, root user: #

🎨 Color Settings (\033[XXm)

In this setting, colors are applied to the username, current directory, and Git branch name.

Code Color Scope of Application
\[\033[1;32m\] Green Username (\u)
\[\033[1;34m\] Blue Current Directory (\w)
\[\033[1;31m\] Red Git Branch ($(__git_ps1))
\[\033[00m\] Reset to Default Color Resets the color so it doesn't affect the following characters

PS1 formatting has various options, but I will omit the details here.
If you are interested, please look into it (you can find information by searching for things like "bash PS1 customization").


3. Displaying Git status in the prompt

While not mandatory, in addition to the settings above, you can display the Git working status in the prompt by adding the following constants.

GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWSTASHSTATE=1
GIT_PS1_SHOWUNTRACKEDFILES=1
GIT_PS1_SHOWUPSTREAM="auto"
GIT_PS1_STATESEPARATOR="|"
GIT_PS1_SHOWCOLORHINTS=yes
GIT_PS1_SHOWCONFLICTSTATE=yes
GIT_PS1_HIDE_IF_PWD_IGNORED=yes

🔧 List of constants available in git-prompt.sh

Constant Description Value Default behavior if not set
GIT_PS1_SHOWDIRTYSTATE Displays the modification state of the working tree (*, +) 1 (Enabled) Does not display modification state
GIT_PS1_SHOWSTASHSTATE Displays whether there are changes stashed via git stash 1 (Enabled) Does not display git stash information
GIT_PS1_SHOWUNTRACKEDFILES Displays untracked files (%) 1 (Enabled) Does not display untracked files
GIT_PS1_SHOWUPSTREAM Displays the difference with the remote (<, >, <>, =) "auto", "verbose" Does not display the difference with the remote
GIT_PS1_STATESEPARATOR Changes the symbol that separates states `" ", " / "`, etc.
GIT_PS1_SHOWCOLORHINTS Displays the Git branch name and status in color yes (Enabled) Plain display (if PS1 specifies a color, that color is used)
GIT_PS1_SHOWCONFLICTSTATE Displays CONFLICT yes (Enabled) Does not display conflict information
GIT_PS1_HIDE_IF_PWD_IGNORED Hides Git information if the current directory is ignored by .gitignore yes (Enabled) Displays Git information regardless of .gitignore

📌GIT_PS1_SHOWDIRTYSTATE
If there are changes in the working tree, this status is displayed in the prompt.
To enable this, set it to 1.

  • * → Unstaged changes present
  • + → Staged changes present

Display example:

user@host:~/repo (main *) $
user@host:~/repo (main *+) $

📌GIT_PS1_SHOWSTASHSTATE
Displays $ if there are changes saved in git stash.

Display example:

user@host:~/repo (main $) $

📌GIT_PS1_SHOWUNTRACKEDFILES
Displays % if there are untracked files.

Display example:

user@host:~/repo (main%) $

📌GIT_PS1_SHOWUPSTREAM
Indicates the difference with the remote.

  • < → Behind the remote
  • > → Ahead of the remote
  • <> → Local and remote have diverged
  • = → In sync with the remote

Display example (when set to auto):

user@host:~/repo (main <) $  # Behind the remote
user@host:~/repo (main >) $  # Ahead of the remote
user@host:~/repo (main <>) $ # Local and remote have different commits
user@host:~/repo (main =) $  # In sync with the remote

Regarding the setting value of GIT_PS1_SHOWUPSTREAM, generally setting it to auto is fine.
I plan to explain more detailed settings (verbose, name, etc.) in a separate article.


📌GIT_PS1_SHOWSTASHSTATE
Displays $ if there are changes saved in git stash.

Display example:

user@host:~/repo (main $) $

📌GIT_PS1_STATESEPARATOR
You can change the symbol that separates the states to something like "|" or "/" (default is a space).

Display example (when set to "|"):

user@host:~/repo (main|+) $

📌GIT_PS1_SHOWCOLORHINTS

Displays the Git branch name and status information in color based on the Bash color scheme.
For example, it makes the situation intuitively easy to understand with the branch name in green, changes in red, and differences from the remote in yellow.

While you can also specify colors in PS1, if both are set, GIT_PS1_SHOWCOLORHINTS takes precedence.


📌GIT_PS1_SHOWCONFLICTSTATE

Displays CONFLICT if there are unresolved conflicts.

Display example:

user@host:~/repo (main CONFLICT) $

📌GIT_PS1_HIDE_IF_PWD_IGNORED
Prevents Git information from being displayed in the prompt if the current directory is ignored by .gitignore.
This is useful for preventing Git information from being shown in unnecessary directories.

Behavior after activation:

  • Git branch and status will no longer be displayed in directories ignored by .gitignore.
  • Git information will be displayed as usual in directories not covered by .gitignore.

Summary

  • Using git-prompt.sh allows you to display the current branch and working status in the prompt.
  • It can be easily implemented by simply adding settings to .bashrc.

Discussion