magicmonty / bash-git-prompt

An informative and fancy bash prompt for Git users
BSD 2-Clause "Simplified" License
6.72k stars 965 forks source link

Informative git prompt for bash and fish

Gitter

This prompt is a port of the "Informative git prompt for zsh" which you can find here

A bash prompt that displays information about the current git repository. In particular the branch name, difference with remote branch, number of files staged, changed, etc.

gitstatus.sh and git-prompt-help.sh added by AKS.

Examples

The prompt may look like the following:

Example prompt

Prompt Structure

By default, the general appearance of the prompt is::

(<branch> <upstream branch> <branch tracking>|<local status>)

The symbols are as follows:

Installation

via Homebrew on Mac OS X

if [ -f "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh" ]; then
  __GIT_PROMPT_DIR=$(brew --prefix)/opt/bash-git-prompt/share
  GIT_PROMPT_ONLY_IN_REPO=1
  source "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh"
fi

via Git clone

git clone https://github.com/magicmonty/bash-git-prompt.git ~/.bash-git-prompt --depth=1

Add to the ~/.bashrc:

if [ -f "$HOME/.bash-git-prompt/gitprompt.sh" ]; then
    GIT_PROMPT_ONLY_IN_REPO=1
    source "$HOME/.bash-git-prompt/gitprompt.sh"
fi

install for the fish shell

sudo install -m 666 gitprompt.fish /usr/share/fish/tools/web_config/sample_prompts/
fish_config

 to install the bash-git-prompt as a choice under the prompt tab of the web config. Selecting this will copy it to ~/.config/fish/functions/fish_prompt.fish

mkdir -p ~/.config/fish/functions/
cp gitprompt.fish ~/.config/fish/functions/fish_prompt.fish

 to overwrite the current prompt with the bash-git-prompt directly

All configs for .bashrc

# Set config variables first
GIT_PROMPT_ONLY_IN_REPO=1

# GIT_PROMPT_FETCH_REMOTE_STATUS=0   # uncomment to avoid fetching remote status
# GIT_PROMPT_IGNORE_SUBMODULES=1 # uncomment to avoid searching for changed files in submodules
# GIT_PROMPT_WITH_VIRTUAL_ENV=0 # uncomment to avoid setting virtual environment infos for node/python/conda environments
# GIT_PROMPT_VIRTUAL_ENV_AFTER_PROMPT=1 # uncomment to place virtual environment infos between prompt and git status (instead of left to the prompt)

# GIT_PROMPT_SHOW_UPSTREAM=1 # uncomment to show upstream tracking branch
# GIT_PROMPT_SHOW_UNTRACKED_FILES=normal # can be no, normal or all; determines counting of untracked files

# GIT_PROMPT_SHOW_CHANGED_FILES_COUNT=0 # uncomment to avoid printing the number of changed files

# GIT_PROMPT_STATUS_COMMAND=gitstatus_pre-1.7.10.sh # uncomment to support Git older than 1.7.10

# GIT_PROMPT_START=...    # uncomment for custom prompt start sequence
# GIT_PROMPT_END=...      # uncomment for custom prompt end sequence

# as last entry source the gitprompt script
# GIT_PROMPT_THEME=Custom # use custom theme specified in file GIT_PROMPT_THEME_FILE (default ~/.git-prompt-colors.sh)
# GIT_PROMPT_THEME_FILE=~/.git-prompt-colors.sh
# GIT_PROMPT_THEME=Solarized # use theme optimized for solarized color scheme
source ~/.bash-git-prompt/gitprompt.sh

You can set the GIT_PROMPT_SHOW_UNTRACKED_FILES variable to no or normal to speed things up if you have lots of untracked files in your repository. This can be the case for build systems that put their build artifacts in the subdirectory structure of the git repository. Setting it to all will count all untracked files, including files listed in .gitignore.

Themes

The most settings are now stored in theme files. To select a theme, set the variable GIT_PROMPT_THEME to the name of the theme located in <INSTALLDIR>/themes without the extension .bgptheme like this:

GIT_PROMPT_THEME=Solarized

If you set GIT_PROMPT_THEME to Custom, then the .git-prompt-colors.sh in the home directory will be used. This file can now be generated with the command git_prompt_make_custom_theme [<Name of base theme>]. If the name of the base theme is ommitted or the theme file is not found, then the Default theme is used. If you have already a custom .git-prompt-colors.sh in your home directory, an error message will be shown.

You can display a list of available themes with git_prompt_list_themes (the current theme is highlighted)

If you omit the GIT_PROMPT_THEME variable, the Default theme is used or, if you have a custom .git-prompt-colors.sh in your home directory, then the Custom theme is used.

Ubuntu Themes

Ubuntu requires a bit more spacing for some characters so it has its own themes.

These can be listed with git_prompt_list_themes:

git_prompt_list_themes | grep Ubuntu
Theme structure

Please see the Custom.bgptemplate in the themes subdirectory of the installation directory!

A theme consists of a function override_git_prompt_colors() which defines at least the variable GIT_PROMPT_THEME_NAME with a unique theme identifier and a call to the function reload_git_prompt_colors <ThemeName> like follows:

override_git_prompt_colors() {
  GIT_PROMPT_THEME_NAME="Custom" # needed for reload optimization, should be unique

  # Place your overrides here
  ...
}

# load the theme
reload_git_prompt_colors "Custom"

The advantage of this approach is, that you only need to specify the parts, that are different to the Default theme.

If you use a custom theme in .git-prompt-colors.sh, please set GIT_PROMPT_THEME_NAME="Custom".

Further customizations

prompt_callback() {
    if [ $(jobs | wc -l) -ne 0 ]; then
        echo -n " jobs:\j"
    fi
}
  GIT_PROMPT_START_USER="_LAST_COMMAND_INDICATOR_ ${Yellow}${PathShort}${ResetColor}"
  GIT_PROMPT_START_ROOT="_LAST_COMMAND_INDICATOR_ ${GIT_PROMPT_START_USER}"

If you want to display the exit code too, you can use the placeholder _LAST_COMMAND_STATE_ in GIT_PROMPT_COMMAND_OK or GIT_PROMPT_COMMAND_FAIL in your .git-prompt-colors.sh:

GIT_PROMPT_COMMAND_OK="${Green}✔ " # displays as ✔
GIT_PROMPT_COMMAND_FAIL="${Red}✘-_LAST_COMMAND_STATE_ " # displays as ✘-1 for exit code 1
git_prompt_reset
git_prompt_toggle

Enjoy!

Alternative RPM Install

This project ships an RPM spec to simplify installation on RHEL and clones. If you wish to install from RPM, you may first build the RPM from scratch by following this procedure:

git tag -a -m "Tag release 1.1" 1.1
VER=$(git describe)
# replace dash with underscore to work around
# rpmbuild does not allow dash in version string
VER=${VER//\-/_}
git archive                                \
    --format tar                           \
    --prefix=bash-git-prompt-${VER}/       \
    HEAD                                   \
    --  *.sh                               \
        *.fish                             \
        LICENSE.txt                        \
        README.md                          \
        themes                             \
    > bash-git-prompt-${VER}.tar
mkdir -p /tmp/bash-git-prompt-${VER}
sed "s/Version:.*/Version:        ${VER}/"          \
    bash-git-prompt.spec                            \
    > /tmp/bash-git-prompt-${VER}/bash-git-prompt.spec
OLDDIR=$(pwd)
cd /tmp
tar -uf ${OLDDIR}/bash-git-prompt-${VER}.tar      \
        bash-git-prompt-${VER}/bash-git-prompt.spec
cd ${OLDDIR}
gzip bash-git-prompt-${VER}.tar
mv bash-git-prompt-${VER}.tar.gz bash-git-prompt-${VER}.tgz
rpmbuild -ta bash-git-prompt-xxx.tar.gz

Then you may publish or install the rpm from "~/rpmbuild/RPMS/noarch".

License

This code is under the BSD 2 Clause (NetBSD) license.

Who Are You?

The current maintainer of the original bash-git-prompt is Martin Gondermann.

Contributing

If you want to contribute you can look for issues with the label up-for-grabs. Please leave a comment on the issue, that you want to fix it, so others know, the labels are "taken".

Pull requests are welcome. I will check them and merge them, if I think they help the project.

Similar projects

Donations

I accept tips through Flattr.

Flattr