News for hackers. Stuff that matters.
Re:Display your current git branch as part of your bash prompt by zain on Sunday August 16, 2009 @09:37PM (#767157)
Cool! I've practically created my own framework in Bash to hack my prompt but I never thought about sharing it.

My prompt shows the git branch, svn revision, and active virtualenv ("workon" and "deactivate" are virtualenv commands in the screenshot below). Also, mine has COLORS! (Colors within the dynamic function are much harder than they sound)

Here's a quick screenshot: http://img.skitch.com/20090817-gtp7bgkmdn14j84ryhepa2edke.pn...

You can extend it pretty easily to show whatever you want by doing little more than adding another function. Throw this into ~/.profile if you want to try it: http://pastebin.com/f79a9af51

Re:Display your current git branch as part of your bash prompt by kd5bjo on Sunday August 16, 2009 @08:58PM (#767107)
Emmett and I figured this out the other day at the request of one of our other engineers; I thought that some of you might be interested in it.
Re:Display your current git branch as part of your bash prompt by decklin on Sunday August 16, 2009 @10:07PM (#767194)
It's better to use a plumbing command for this sort of thing; git-completion.bash (http://git.kernel.org/?p=git/git.git;a=blob;f=contrib/comple...) has a thorough example. (The actual command called is 'git symbolic-ref HEAD'.)
Re:Display your current git branch as part of your bash prompt by kd5bjo on Sunday August 16, 2009 @10:29PM (#767211)
That's definitely a lot more full-featured, but seems like overkill for what I was trying to do.

Had I known about `git symbolic-ref`, I might have used that instead of `git branch`. On the other hand, I would have needed to re-implement lots of what `git branch` does, such as detecting the difference between not being in a git repository and simply not being on a branch.

The interface of `git branch` seems stable enough that I doubt it will break this. If it does, it should take me no longer than 5 minutes to fix, since that's about how long it would take me to re-write the whole thing from scratch.

Is there any particular reason that using `git symbolic-ref` is better than `git branch` in this case, given that I want human-readable output, instead an unambiguous filesystem location?

Re:Display your current git branch as part of your bash prompt by decklin on Sunday August 16, 2009 @11:25PM (#767279)
Personally, I'd argue you do want to keep track of whether you are in a repo or not, since including that pipeline in your PS1 always forks three processes on every prompt. And if you're going to do that in your shell, it's good to have separate commands for each task.

For the case of not being on a branch, the output of 'git branch' is not very useful. I am currently using 'git name-rev --name-only HEAD', but the contrib script has an even fancier mechanism (via 'git describe') for coming up with something to display when HEAD isn't a symbolic ref.

Re:Display your current git branch as part of your bash prompt by davidmathers on Monday August 17, 2009 @12:07AM (#767336)
Try this:

  export PS1='\w:$(__git_ps1 "%s")$ '
EDIT - here's mine btw:

  function parse_git_dirty {
    git diff --quiet HEAD &>/dev/null
    [[ $? == 1 ]] && echo "✎ " || echo "➜ "
  }
  function parse_git_branch {
    local branch=$(__git_ps1 "%s")
    [[ $branch ]] && echo "$branch$(parse_git_dirty)"
  }
  PS1='\u@\h $(parse_git_branch)\W $ '
If the branch has local changes it shows the pencil, in addition to the branch name. I'm still experimenting.
Re:Display your current git branch as part of your bash prompt by joshwa on Sunday August 16, 2009 @11:07PM (#767254)
git-completion.bash has the added benefit of giving you tab-completion on git commands, and arguments to git commands.
Re:Display your current git branch as part of your bash prompt by vicaya on Monday August 17, 2009 @05:11PM (#769117)
It also shows the correct state of merge/rebase in the prompt as well. In many distributions, it's installed as /etc/bash_completion.d/git when you install git from a package management system, you just need to source /etc/bash_completion to get all the benefits.

The OP is an example of wheel reinvented, poorly.

Re:Display your current git branch as part of your bash prompt by eapen on Sunday August 16, 2009 @09:32PM (#767151)
This is cool.. but how do I undo this?
Re:Display your current git branch as part of your bash prompt by kd5bjo on Sunday August 16, 2009 @09:36PM (#767153)
You can set your prompt to whatever you want by running `export PS1=...`. What the default is depends on which version of unix you're running. Often, you can get your original prompt back by running `source ~/.bashrc`.

Barring that, logging out and back in will definitely fix it, as long as you didn't put it in one of your bash config files.

Re:Display your current git branch as part of your bash prompt by chrischen on Sunday August 16, 2009 @09:30PM (#767150)
cool, thanks.
Re:Display your current git branch as part of your bash prompt by sandGorgon on Monday August 17, 2009 @02:22AM (#767485)
zsh does a much better job (with its right prompts and formatting) http://img194.imageshack.us/img194/2101/zsh1.png - this actually shows a git repository imported from svn

http://img40.imageshack.us/img40/3738/zsh2.png - this shows a regular svn repo

This is what you need in your zshrc

# precmd is called just before the prompt is printed function precmd() { title “zsh” “$USER@%m” “%55<…<%~” psvar=() vcs_info [[ -n $vcs_info_msg_0_ ]] && psvar[1]=”$vcs_info_msg_0_” }

vcs_info export RPS1=”${YELLOW}%1v${NORM}”

Re:Display your current git branch as part of your bash prompt by PStamatiou on Sunday August 16, 2009 @11:00PM (#767247)
Isn't this also in a public github guide? http://github.com/guides/put-your-git-branch-name-in-your-sh...

Been using it for a while now. ;-)

Re:Display your current git branch as part of your bash prompt by oldgregg on Sunday August 16, 2009 @11:29PM (#767284)
Funny, I wrote this .bash_profile script (http://gist.github.com/168286) just the other day because I wanted... 1) Minimalist colors 2) Stripped down CLEAN prompt 3) For GIT folders it will display the current branch.

It was based on this which I thought was too ugly: http://www.simplisticcomplexity.com/2008/03/13/show-your-git...

Also, if you use git+github+capistrano, I wrote a bash script where you type "tap" and it will add all new files, commit, push to github. If you type "tap /d" it will also cap deploy it to the server and reload in nginx passenger. For small projects I'll push something and then have to make a small change. Now I can just edit the file in textmate and can add-commit-push-deploy with one command.

http://gist.github.com/168589

Re:Display your current git branch as part of your bash prompt by mbrubeck on Monday August 17, 2009 @11:50AM (#768322)
I use Ryan Tomayko's git-sh, a "git mode" for Bash that includes the branch in the prompt, and also adds the most common git commands as top-level commands (so you can type "checkout master" instead of "git checkout master"):

http://github.com/rtomayko/git-sh/tree/master

Re:Display your current git branch as part of your bash prompt by xgamerx on Monday August 17, 2009 @05:03AM (#767673)
I reworked this script to show your branch name in green if the branch is clean and red if the branch is dirty. Personal preference but I prefer the visual color distinction denoting branch state.

http://gist.github.com/120804

Re:Display your current git branch as part of your bash prompt by jackdawjack on Sunday August 16, 2009 @11:31PM (#767289)
I liked this but it really slowed things down. Perhaps because my home folder etc are all on nfs shares?
Re:Display your current git branch as part of your bash prompt by llimllib on Sunday August 16, 2009 @09:59PM (#767186)
Totally do this; I can't live without it.

Also, display your current branch in mercurial repos: http://stevelosh.com/blog/entry/2009/3/17/mercurial-bash-pro...

Re:Display your current git branch as part of your bash prompt by lvv on Monday August 17, 2009 @12:00AM (#767330)
You have not seen git-prompt yet. How about forgetting that there is git-status? See http://volnitsky.com/project/git-prompt And it will do svn, hg and a lot of other stuff.
Re:Display your current git branch as part of your bash prompt by codahale on Monday August 17, 2009 @06:18PM (#769250)
Leonid Volnitsky's git-prompt is the best git-enabled Bash script I've found.

http://github.com/lvv/git-prompt/tree/master

Re:Display your current git branch as part of your bash prompt by myth_drannon on Sunday August 16, 2009 @10:32PM (#767216)
It's by default in msysgit bash.