Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display the current branch and folder path in terminal?

I've been watching some of the Team Treehouse videos and they have a very nice looking terminal when working with Git.

For example they have (something similar):

mike@treehouseMac: [/Work/test - feature-branch-name] $ git add . mike@treehouseMac: [/Work/test - feature-branch-name] $ git commit -m "Some feature." mike@treehouseMac: [/Work/test - feature-branch-name] $ git checkout master mike@treehouseMac: [/Work/test - master] $ git status 

How can my terminal show me some useful information of what branch I'm on, with colors to distinguish bits of the data I want? Is there some sort of de-facto plugin I haven't found yet?

I'm using Mac OSX 10.8

like image 428
sergserg Avatar asked Jun 27 '13 02:06

sergserg


2 Answers

For anyone looking for how to do this in macOS Catalina or above (10.15+ incl. Big Sur 11.0) which has deprecated bash in favour of zsh, here is my .zshrc file:

parse_git_branch() {     git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p' } COLOR_DEF='%f' COLOR_USR='%F{243}' COLOR_DIR='%F{197}' COLOR_GIT='%F{39}' NEWLINE=$'\n' setopt PROMPT_SUBST export PROMPT='${COLOR_USR}%n@%M ${COLOR_DIR}%d ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}${NEWLINE}%% ' 

If you don't like the colours I have used, replace the 243/197/39 values with the colour codes as defined here: https://misc.flogisoft.com/bash/tip_colors_and_formatting

like image 86
romiem Avatar answered Oct 01 '22 05:10

romiem


Simple way

Open ~/.bash_profile in your favorite editor and add the following content to the bottom.

Git branch in prompt.

parse_git_branch() {     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' }  export PS1="\u@\h \[\033[32m\]\w - \$(parse_git_branch)\[\033[00m\] $ " 

Add Git Branch To Terminal Prompt (Mac)

like image 45
6LYTH3 Avatar answered Oct 01 '22 04:10

6LYTH3