有没有办法在 Mac 的终端提示中添加当前工作目录/当前 git 存储库/分支

发布于 2024-12-06 06:13:52 字数 435 浏览 0 评论 0原文

第一次海报。

这在本周的工作对话中出现了......

有没有办法,当你远程连接到 git 时,你可以在终端提示符中显示当前工作目录/当前 git repo/branch ?

显然,有针对 Linux 用户的 linux/vim 脚本,我想将这种 shell 脚本添加到我的配置文件中。

目前我正在使用此页面中的一些信息 http://sos.blog-city.com/mac_os_x__bash_customize_your_terminal_prompt_a_little_color .htm 在本地处理其中一些信息。

提前致谢。

first time poster.

This came up in conversation at work this week...

Is there a way, when you connect to git remotely that you can get display current working directory/current git repo/branch in your terminal prompt?

Apparently, there are linux/vim scripts that exist for linux users, and I'd like to add this sort of shell script to my profile.

Currently I'm using some info from this page http://sos.blog-city.com/mac_os_x__bash_customize_your_terminal_prompt_a_little_color.htm to address some of this info locally.

Thanks in advance.

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

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

发布评论

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

评论(4

嘿哥们儿 2024-12-13 06:13:52

获取 git 完成脚本的副本。你可以从 git 本身获取它,或者如果你有一个方便的 Linux 盒子,你甚至可以从那里复制它(它可能是 /etc/bash_completion.d/git)。然后,安排它通过 bash“获取”。您可以通过在 .bashrc 中添加类似的内容来完成此操作:(

. /usr/local/git-completion

假设您在 Mac 上将文件命名为 /usr/local/git-completion)。

最后,您需要调整提示。另外,在您的 .bashrc 中,添加如下内容:

export PS1='[\w$(__git_ps1 "|%s")]\$ '

这是一篇博客文章(不是我写的),更详细地讨论了此问题(以及其他一些相关内容):http://blog.bit Fluent.com/post/27983389/git-utilities-you-cant-live-without

Get a copy of the git completion script. You can get this from git itself, or if you have a Linux box handy you could even just copy it from there (it'll probably be /etc/bash_completion.d/git). Then, arrange for this to get "sourced" by bash. You can do this by adding something like this to your .bashrc:

. /usr/local/git-completion

(assuming you named the file /usr/local/git-completion on your Mac).

Finally, you'll want to adjust your prompt. Also in your .bashrc, add something like:

export PS1='[\w$(__git_ps1 "|%s")]\$ '

Here's a blog post (not by me) that talks about this (and some other related stuff) in more detail: http://blog.bitfluent.com/post/27983389/git-utilities-you-cant-live-without

遥远的她 2024-12-13 06:13:52

好的,在你为我指明正确的方向后,我尝试了这个,我的谷歌搜索得到了更精确的结果。

很多人都指向您与我分享的帖子,如下所示:https://superuser.com/questions/31744/how-to-get-git-completion-bash-to-work-on-mac-os-x 但我发现其他一些像这样的珠宝,我没有使用,但信息丰富:jeetworks.org/node/10,jonmaddox.com/2008/03/13/show-your-git-branch-name-in-your-prompt/。

我需要一些关于安装 git.completion 的不同指导,因为我使用在这里找到的自制软件: https://github.com/bobthecow/git-flow-completion/wiki/Install-Bash-git-completion 其中涵盖了几种安装方法。

最后,我的 bash/终端有点迟钝,所以在我使用这些指令之前,我按照这些说明升级到了最新的 bash:milkbox.net/brace_completion_snow_leopard_upgrading_bash/ 并获得了一些很大的速度改进。

我最终不得不非常仔细地重建我的配置文件脚本,但有线索和错误(因为 Bash 3 和 4 之间的差异,以及一些语法错误)——现在它看起来很棒,并且可以完成工作。

再次感谢。

对于上述网站的安全限制,我们深表歉意,限制我(因为我是新手)只能使用 2 个链接来打击垃圾邮件。

OK, I experimented with this after you pointed me in the right direction, my google searches got more refined results.

A lot of people point to the post you shared with me, like here: https://superuser.com/questions/31744/how-to-get-git-completion-bash-to-work-on-mac-os-x but I found some other jewels like these, which I didn't use but were informative: jeetworks.org/node/10 , jonmaddox.com/2008/03/13/show-your-git-branch-name-in-your-prompt/ .

I needed some different guidance on installing git.completion because I use homebrew which I found here: https://github.com/bobthecow/git-flow-completion/wiki/Install-Bash-git-completion which covers several ways to install it.

Finally, my bash/terminal has been a bit pokey so I upgraded to the latest bash with these instructions before I meshed with any of this: milkbox.net/brace_completion_snow_leopard_upgrading_bash/ and got some great speed improvement.

I ended up having to rebuild my profile script very carefully but with trail and error (because of differences between Bash 3 and 4, and some syntax errors)- now it looks great, and does the job.

Thanks again.

Sorry about above the security restraints of the site, restrict me (since I'm a newb) to 2 links to combat spam.

孤君无依 2024-12-13 06:13:52

您只需要 2 个步骤即可完成。

步骤1:

在您喜欢的编辑器中打开~/.bash_profile并将以下内容添加到底部。

对我来说就像

emacs ~/.bash_profile

Step.2:

将以下内容添加到底部。

function git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'}

export PS1='\h:\w$(git_branch) \u\

完毕!

PS:

如果你想让你的终端色彩缤纷,请尝试以下内容。

export PS1 = '\[\e[1;39m\]\h:\w\[\e[01;39m\]$(git_branch) \[\e[01;39m\]$ \[\e[0m\]'

完毕!

PS:

如果你想让你的终端色彩缤纷,请尝试以下内容。

You just need 2 steps to do it.

Step.1:

Open ~/.bash_profile in your favorite editor and add the following content to the bottom.

For me it is like

emacs ~/.bash_profile

Step.2:

Add the following content to the bottom.

function git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'}

export PS1='\h:\w$(git_branch) \u\

Done!

P.s:

If you want your terminal colorful, try the following content.

export PS1 = '\[\e[1;39m\]\h:\w\[\e[01;39m\]$(git_branch) \[\e[01;39m\]$ \[\e[0m\]'

Done!

P.s:

If you want your terminal colorful, try the following content.

∝单色的世界 2024-12-13 06:13:52

在 shell 提示符中获取 git 分支/状态信息的另一种选择是使用 powerline-shell

您可以在这个屏幕截图中看到它的样子:
在此处输入图像描述

洋红色/绿色条是当前分支名称。它的颜色表示工作目录是否干净。当存在未跟踪的文件或有要从上游远程拉取/推送到上游远程的提交时,还会出现标记。

Powerline-shell 与 bash、zsh 和 Fish 兼容。

Another option to get git branch/status info in your shell prompt is to use powerline-shell.

You can see what this looks like in this screenshot:
enter image description here

The magenta/green bar is the current branch name. Its color indicates whether the working directory is clean. There are also markers that appear when there are untracked files, or when there are commits to be pulled-from/pushed-to the upstream remote.

Powerline-shell is compatible with bash, zsh, and fish.

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