So, we're having a great time with git, fighting over how branches should work and what policy is best in our environment, which I suppose I'll talk about in another post, but for today, I wanted to share a little bit of usefulness.

Most of our repositories have several branches for various releases and its often confusing and terse to 1. remember where you are and 2, run git branch to see.


This for example, I have changed to my repository in GitHub, but unless you know that you're in a repository you couldn't be sure.  Long story short, lets say we've had one or two boo-boos along the way. 

Now, imported projects in eclipse will have the branch of the code you're working on so today, we're going to bring it to the terminal.  Cut the following and put it in your .bashrc or .bash_profile and see your repository and branch pop out on the prompt.  Now,  I have decorated the prompt with some colours, which you might want to change.  (search for bash unix colors for details)

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

parse_git_repo() {
     git rev-parse --show-toplevel 2> /dev/null | xargs basename| sed 's/\(.*\)/[\1]/'
}

export PS1="\[\033[91m\]\u\[\033[30m\]@\[\033[34m\]\h[\[\033[36m\]\w\[\033[30m\]] \
\n\r\[\033[31m\]\$(parse_git_repo)\[\033[00m\]-\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

What you get from all this, the next time you start a terminal is this (without my adornments).

So, as soon as you enter a repository, the prompt now tells you which repository and which branch you are on too, in this case I have branch called flatten which I used for the previous post.  
Finally, if you dont want the colour, strip the escape codes and while your PS1 export will look more sane, it'll lack a certain bedazzling...

export PS1="\u@\h[\w] \n\r\$(parse_git_repo)-\$(parse_git_branch) $ "


Have fun!
1

View comments

About Me
About Me
My Blog List
My Blog List
Page Views
Page Views
9 3 5 6 6 8
Subscribe
Subscribe
Blog Archive
Interesting Links
Loading
Dynamic Views theme. Theme images by Jason Morrow. Powered by Blogger.