Skip to content

Instantly share code, notes, and snippets.

@sideshowcoder
Created September 27, 2017 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sideshowcoder/4fe517741194a6ac0e6f683e3a3a2af2 to your computer and use it in GitHub Desktop.
Save sideshowcoder/4fe517741194a6ac0e6f683e3a3a2af2 to your computer and use it in GitHub Desktop.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \w\$(parse_git_branch) \\$ "
@yarmand
Copy link

yarmand commented Sep 28, 2017

Some additions ;-)

1- use single quote on PS1 for cleaner syntax
2- path display: If you are in a git repository, it will replace the path by:
[root_of_your_repo]/relative/path
3- when you have modified your repo, it display [!] after the name of the branch

enjoy !

 git_subdir_or_path() {
  repo_root=$(git_repo_root)
 if [ -n "$repo_root" ] ; then
  repo__basename="[$(basename $repo_root)]"
  echo "${PWD/$repo_root/$repo__basename}"
 else
  echo "${PWD/#$HOME/~}"
 fi
}

git_repo_root(){
  git rev-parse --show-toplevel 2>/dev/null
}
parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

git_tainted() {
  if [ -n "$(git status -s)" ];  then
    echo '[!]'
  fi
}

export PS1='\u@\h $(git_subdir_or_path)$(parse_git_branch)$(git_tainted)\$ '

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