tags:

views:

399

answers:

5

Torvalds seems to have the following prompt.

[torvalds@g5 git]$

The first word is username. g5 seems to be a branch in Git repo, while git shows that it is a branch in Git.

My current prompt

PROMPT="$"

How can you have a similar prompt as Torvalds'?

+3  A: 

Actually, I'm guessing that g5 refers to the hostname of the machine he is currently working on, and git is the current working directory. The format [user@hostname dir]$ is a pretty standard (i.e., widely-used) shell prompt.

mipadi
+2  A: 

Git's integration with Bash programmable completion provides a function named __git_ps1.

If you change your PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ ' (in your .bashrc or some other interactively-sourced file), and do no further customizations, your prompt will look like this:

[user@host ~]$ cd /usr/src/linux
[user@host linux ((v2.6.30))]$
ephemient
The question is asking about zsh, not bash.
graywh
+1  A: 

Like ephemient said, you will want to have that Git bash script installed, installation instructions are near the top of the file. You might also want to check out the Github guide page for this. One thing worth noting is that the branch will only show up if you are in a git directory. For example, this is what my normal prompt looks like: blaenk@macbook:~ $ and the prompt looks like this when I am in a git directory: blaenk@macbook:~/code/iphone/DIFM (master*)$

If you look closely, the part where it shows the branch, master, has an asterisk after it. This signifies that there are unstaged changes; it will show a + if changes are staged. This can be pretty helpful. To do this, you basically have to set GIT_PS1_SHOWSTASHSTATE to a non-empty state. So for example in your ~/.bashrc or ~/.bash_profile, put the following:

export GIT_PS1_SHOWDIRTYSTATE=true

Now when you go to a git directory, you should see the indicator if there are any unstaged changes or if there are any staged changes. You can test this out really quick by editing a file. The asterisk should show up. You can then restore the file to its original state by doing:

git checkout -- the/file.txt

By the way, that auto complete bash script is also really awesome. You can finally do stuff like 'git chec' then press TAB, and it'll autocomplete to checkout for example, and you can also auto complete branch names too.

Some other resources you will most likely be interested in are the following, which guide you through the process of shaping your prompt the way you want it, and if you want, adding color to certain parts, which can make for a much more readable and informative prompt. Just try not to overdo it.

Jorge Israel Peña
Also: http://www.simplicidade.org/notes/archives/2008/02/git_bash_comple.html
Jakub Narębski
+2  A: 

If you use zsh (rather than more popular bash), take a look at VCS info in prompts blog post by Xana Yammering about using vcs_info subsystem developed by Frank Terbeck for zsh, with backend for Git.

Jakub Narębski
A: 

I get the following finally work

 function get_git_branch { 
   git branch | awk '/^\*/ { print $2 }
 }
 function get_git_dirty { 
   git diff --quiet || echo '*'
 }
 function get_git_prompt { 
   git branch &> /dev/null || return 1 
   echo "($(get_git_branch)$(get_git_dirty)) "
 }
 PROMPT="$(get_git_prompt)\$ "

Source

Masi
The prompt do not work anymore for some unknown reason. I made the following prompt to substitute it http://stackoverflow.com/questions/1128496/to-get-a-prompt-which-indicates-git-branch-in-zsh/1128721#1128721
Masi
Don't use `git branch` to get current branch name, it is porcelain: use `git symbolic-ref HEAD`.
Jakub Narębski