v3rspg
Last Updated: November 09, 2017
·
12
· hansnans

gitbash.bashrc

Git branch on Bash

This script will show up the branch if you currently are in a git repository in your bash

If you want to add this (and other) script(s) to your bashrc routine you could:
1. Add the following lines to your ~/.bashrc
2. Create the directory ~/.bashrc.d/
3. copy this file (and other?) to this folder

if [ -d ~/.bashrc.d ]; then
    for file in $(/bin/ls ~/.bashrc.d/*.bashrc); do
        . $file;
    done
fi

You could also just add the following code in your .bashrc itself

#!/bin/bash
# check if directory belongs to a git repo
# if it does, errorcode ($?) will be 0
function isgit() {
        git worktree list > /dev/null 2>&1
}

function git_branch() {
        isgit # check if folder is in a git repo
        if [[ $? == 0 ]]; then # check error code of last command
        # __git_ps1 puts the branch surrounded with round braces (())
        # the sed commmand replaces () braces with [] braces.
                echo "$(__git_ps1)" | sed -e 's/(\(.*\))/[\1]/' 
        fi
}

# PS1 string will be set here. Special is only the $(git_branch).
# If we are in a repo it will return a colorized string with the branch
# otherwise it will have no affect
export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\e[00m\]\[\e[1;36m\]$(git_branch)\[\e[00m\]$ '

And this is what it looks like
Auswahl_002.png

Say Thanks
Respond