Torvalds 似乎有以下提示。
[torvalds@g5 git]$
第一个词是用户名。g5
似乎是 Git repo 中的一个分支,而git
表明它是 Git 中的一个分支。
我当前的提示
PROMPT="$"
你怎么会有和 Torvalds 类似的提示?
实际上,我猜这g5
指的是他当前正在处理的机器的主机名,并且git
是当前的工作目录。该格式[user@hostname dir]$
是一个非常标准的(即广泛使用的)shell 提示符。
Git 与 Bash 可编程补全的集成提供了一个名为__git_ps1
.
如果您更改您的PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
(在您的.bashrc
或其他一些交互式源文件中),并且不做进一步的自定义,您的提示将如下所示:
[user@host ~]$ cd /usr/src/linux [user@host linux ((v2.6.30))]$
如果您使用zsh(而不是更流行的 bash),请查看Xana Yammering 的有关使用Frank Terbeck 为 zsh 开发的子系统和 Git 后端的提示博客文章中的 VCS 信息。vcs_info
就像 ehemient 说的,你会想要安装 Git bash 脚本,安装说明在文件顶部附近。您可能还想查看Github 指南页面。值得注意的一件事是,分支只会在您位于 git 目录中时显示。例如,我的正常提示是这样的:blaenk@macbook:~ $
当我在 git 目录中时,提示是这样的: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.
在搜索过程中遇到了这个问题。只是想为此分享一个更新的解决方案。
Liquid Prompt允许对 zsh 提示进行许多自定义,包括显示 git 分支和针对 git 存储库的不同状态的各种颜色。
我终于得到了以下工作
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)\$ "