To have extra info at a prompt for Git by Zsh

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'?

Upvotes: 0

Views: 2482

Answers (6)

kenchew
kenchew

Reputation: 334

Came across this question during a search. Just thought to share a newer solution for this.

Liquid Prompt allows many customization to the zsh prompt, including showing the git branch and various colorization for different status of the git repository.

Upvotes: 0

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

Upvotes: -1

Jakub Narębski
Jakub Narębski

Reputation: 323454

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.

Upvotes: 2

Jorge Israel Peña
Jorge Israel Peña

Reputation: 38586

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.

Upvotes: 1

ephemient
ephemient

Reputation: 204708

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))]$

Upvotes: 3

mipadi
mipadi

Reputation: 410652

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.

Upvotes: 4

Related Questions