Skip to content

Instantly share code, notes, and snippets.

@petehamilton
Created November 5, 2011 18:07
Show Gist options
  • Save petehamilton/1341831 to your computer and use it in GitHub Desktop.
Save petehamilton/1341831 to your computer and use it in GitHub Desktop.
Nice way of viewing git branch in terminal
#Handy Git stuff
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
function git-track {
CURRENT_BRANCH=$(parse_git_branch)
git-config branch.$CURRENT_BRANCH.remote $1
git-config branch.$CURRENT_BRANCH.merge refs/heads/$CURRENT_BRANCH
}
function parse_git_branch_and_add_brackets {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\ \[\1\]/'
}
PS1="\u@\h:\W\[\033[0;32m\]\$(parse_git_branch_and_add_brackets)\[\033[0m\]\$ "
@HansCz
Copy link

HansCz commented Nov 5, 2011

Nice one... git-track gives me ideas :) Thanks
Just made a new git prompt myself today(most of it is courtesy of wayneeseguin - but I added a few twists too):

function ps1_git () {
  local branch="" 
  local line=""
  local git_prompt="ß"

  shopt -s extglob

  if ! command -v git >/dev/null 2>&1 ; then
    printf "\033[38;5;160m[git not found]\033[0m"
    exit 0
  fi
  while read -r line
  do
    case "${line}" in
      [[=*=]][[:space:]]*)
        branch="${line/[[=*=]][[:space:]]/}"
        ;;
    esac
  done < <(git branch 2>/dev/null)

  if [[ ${branch} ]] ; then

    printf " "

    local git_status="`git status`"
    local git_status_numeric=0

    if [[ "${git_status}" =~ '# Untracked files:' ]] ; then
      printf "\033[38;5;37m·\033[0m"
      let "git_status_numeric+=8"
    fi

    if [[ "${git_status}" =~ '# Changes not staged for commit:' ]] ; then
      printf "\033[38;5;136m·\033[0m"
      let "git_status_numeric+=4"
    fi

    if [[ "${git_status}" =~ '# Changes to be committed:' ]] ; then
      printf "\033[38;5;166m•\033[0m"
      let "git_status_numeric+=2"
    fi

    if [[ "${git_status}" =~ 'nothing to commit (working directory clean)' ]] ; then
      let "git_status_numeric+=1"
    fi          

    case $git_status_numeric in
      1)
        printf "\033[38;5;33m${git_prompt}\033[0m \033[38;5;33m${branch}\033[0m"
        ;;
      2|6|10|14)
        printf "\033[38;5;166m${git_prompt}\033[0m \033[38;5;166m${branch}\033[0m"
        ;;
      4|12)
        printf "\033[38;5;136m${git_prompt}\033[0m \033[38;5;136m${branch}\033[0m"
        ;;
      8)
        printf "\033[38;5;37m${git_prompt}\033[0m \033[38;5;37m${branch}\033[0m"
        ;;

    esac

  fi
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment