在 Zsh 提示下获取 Git 的额外信息

发布于 2024-07-27 16:47:05 字数 390 浏览 13 评论 0原文

Torvalds 似乎有以下提示

[torvalds@g5 git]$ 

第一个词是用户名。 g5 似乎是 Git repo 中的一个分支,而 git 显示它是 Git 中的一个分支。

我当前的提示

PROMPT="$"

你怎么能有与 Torvalds 类似的提示?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

海螺姑娘 2024-08-03 16:47:05

实际上,我猜测 g5 指的是他当前正在使用的机器的主机名,而 git 是当前的工作目录。 [user@hostname dir]$ 格式是一个非常标准的(即广泛使用的)shell 提示符。

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.

夜访吸血鬼 2024-08-03 16:47:05

Git 与 Bash 可编程完成的集成提供了一个名为__git_ps1的函数。

如果您更改您的 PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ ' (在您的 .bashrc 或某些其他交互式源文件),并且不进行进一步的自定义,您的提示将如下所示:

[user@host ~]$ cd /usr/src/linux
[user@host linux ((v2.6.30))]$

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))]$
我很OK 2024-08-03 16:47:05

如果您使用 zsh (而不是更流行的 bash),请查看 提示中的 VCS 信息,由 Xana Yammering 撰写,内容涉及使用 Frank Terbeck 为 zsh 开发的 vcs_info 子系统,后端为 Git。

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.

罪#恶を代价 2024-08-03 16:47:05

就像ephemient所说,你会想要已安装 Git bash 脚本,安装说明位于文件顶部附近。 您可能还想查看 Github 指南页面 中。 值得注意的一件事是,只有当您位于 git 目录中时才会显示分支。 例如,我的正常提示符如下所示: blaenk@macbook:~ $ 当我位于 git 目录中时,提示符如下所示: blaenk@macbook:~/code/ iphone/DIFM (master*)$

如果你仔细观察,它显示分支 master 的部分后面有一个星号。 这意味着存在未阶段性的变化; 如果更改已上演,它将显示 +。 这非常有帮助。 为此,您基本上必须将 GIT_PS1_SHOWSTASHSTATE 设置为非空状态。 因此,例如在您的 ~/.bashrc~/.bash_profile 中,输入以下内容:

export GIT_PS1_SHOWDIRTYSTATE=true

现在,当您转到 git 目录时,您应该看到指示器是否有任何未分阶段的更改或是否有任何分阶段的更改。 您可以通过编辑文件来快速测试这一点。 星号应该出现。 然后,您可以通过执行以下操作将文件恢复到其原始状态:

git checkout -- the/file.txt

顺便说一下,自动完成 bash 脚本也非常棒。 您终于可以执行“git chec”之类的操作,然后按 TAB,它会自动完成结帐,例如,您也可以自动完成分支名称。

您可能最感兴趣的其他一些资源如下,它们将指导您完成按照您想要的方式塑造提示的过程,如果您愿意,还可以为某些部分添加颜色,这可以使提示更具可读性和准确性。信息提示。 只是尽量不要做得太过分。

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.

蓦然回首 2024-08-03 16:47:05

在搜索过程中遇到了这个问题。 只是想为此分享一个更新的解决方案。

Liquid Prompt 允许对 zsh 提示符进行多种自定义,包括显示 git 分支以及针对不同状态的各种着色git 存储库。

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.

海的爱人是光 2024-08-03 16:47:05

我最终得到以下结果

 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)\$ "

来源

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文