Asroads'Blog 君子不器
Mac电脑命令行显示当前git分支和状态
发布于: 2024-04-07 更新于: 2024-04-13 分类于: other 阅读次数: 

前几天在抖音上学习git知识的时候,发现博主的电脑命令行可以直接显示当前仓库的分支名字和状态等信息,一时间,觉得这个功能挺好的,于是今天得空整理一下,设置一下自己的电脑,打开命令行如果目录是git 仓库是可以正常显示为git分支信息啥的。

最终效果

先看一下我本地配置后的效果图:

image-20240407180011875

image-20240407180026229

相关知识链接:

相关说明:上面几个链接都可以配置一天分支状态,最终我选择了 zsh-git-prompt 原因是喜欢它最终展示的样式(颜值喜欢没办法~)

几种方法:

方法一:git官方方式显示git分支

  1. 下载脚本到本地,比如命名为~/.git-prompt.sh下载地址:点击前往

    1
    https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh
  2. 编辑.zshrc: vim ~/.zshrc

1
2
3
4
5
GIT_PS1_SHOWUPSTREAM="auto"
GIT_PS1_SHOWCOLORHINTS="yes"
source ~/.git-prompt.sh
setopt PROMPT_SUBST
PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
  1. 保存配置
1
source ~/.zshrc

方式二:zsh (oh my zsh)+iTerm2

由于自己不太想安装 oh my zsh 这个方案没有过多尝试,大家可以自行搜索查看实现方法。

方式三:使用zsh-git-prompt

  1. 本地下载zsh-git-prompt,将此存储库克隆到硬盘上的某个位置。
1
git clone https://github.com/olivierverdier/zsh-git-prompt.git

image-20240407182137245

  1. 修改文件~/.zshrc,并配置zshrc.sh找到我们的~/.zshrc文件,添加:

    1
    2
    3
    source path/to/zshrc.sh
    # an example prompt
    PROMPT='%B%m%~%b$(git_super_status) %# '

    image-20240407182221176

    比如我这里是:

    image-20240407182244078

    1
    2
    3
    #zsh-git-prompt 插件
    source ~/jsroads/wsplugin/zsh-git-prompt/zshrc.sh
    PROMPT='%B%m%~%b$(git_super_status) %# '
  2. 进入 git 存储库并测试它!

image-20240407182331283

相关知识拓展:

Symbol Meaning
repository clean
●n there are n staged files
✖n there are n unmerged files
✚n there are n changed but unstaged files
there are some untracked files

分支跟踪符号:

Symbol Meaning
↑n ahead of remote by n commits
↓n behind remote by n commits
↓m↑n branches diverged, other by m commits, yours by n commits

其他知识

修改文件权限

  1. sudo -i 切换到root账户
  2. 切换目录 cd 你的文件(夹)路径的上一级目录。
  3. sudo chmod -R 777 你的文件(夹)名。
  4. 输入密码。
  5. 成功

参考

--- 本文结束 The End ---