普通视图

发现新文章,点击刷新页面。
昨天以前首页

zsh | 程序员常用的 git 分支显示

2025年6月27日 20:13

本文主要用于

  1. 记录 终端指令
  2. 记录 zsh 无法设置为系统默认登录 shell => 单独修改每个应用的 默认登录 shell 为 zsh、
  3. 记录 zsh 的功能: 显示 git 分支,自动补全 ,及替代方案

切换 bash zsh

# 打开文件 修改 bash
open -e ~/.bash_profile 
# open 图形界面打开
# ✅ `-e`:是一个选项(option),表示使用默认的文本编辑器打开文件

vim ~/.bash_profile 
# ✅ vim 命令行打开


# 设置默认 shell 为 bash
chsh -s /bin/bash $USER

# 检查是否生效 (如果输出 /bin/bash,说明已经切换成功。
echo $SHELL
# 如果输出 /bin/bash,说明已经切换成功。
#  ✅ `echo`:输出命令,用于将其后面的参数显示在屏幕上
# `$SHELL`:是一个环境变量,存储了当前系统使用的 shell 程序的完整路径

无法修改登录 shell 为 zsh

# ❌ 命令行修改无效
# 切换默认 shell 为 zsh
chsh -s /bin/zsh $USER
# 终端 报错
chsh: Operation was denied because the current credentials do not have the appropriate privileges.
chsh: no changes made

# ❌ 设置无效
# 用“系统设置”图形界面修改(最保险):
# 打开“系统设置” → “用户与群组”
# 点击左下角的锁,输入密码解锁
# 右键你的用户名,选择“高级选项…”
# 找到“登录 shell”,改为 /bin/zsh
# 保存并重启终端
# 弹窗 报错
未能完成操作。(com.apple.extensionKit.errorDomain错误15。)

# 你的 Mac 可能被公司 MDM(移动设备管理)或配置文件限制,禁止更改登录 shell。
# 普通用户(即使是管理员)在某些公司策略下也无法修改 shell。
# 这是 macOS Ventura/Sonoma 等新系统下常见的权限限制报错。

# ✅ 临时使用 zsh
zsh
# 这样当前终端会话会进入 zsh,但关闭窗口后会恢复为 bash。

✅ 手动修改 编辑器 VSCode Cursor , 默认登录 shell 为 zsh

image.png

✅ 手动修改 终端 , 默认登录 shell 为 zsh image.png

✅ 手动修改 iTerm2 , 默认登录 shell 为 zsh image.png

终端显示 git 分支

程序员常用的 git 分支显示方式有以下几种,既美观又简约

1 自定义 Bash PS1(简约风)

这是最基础、最轻量的方式,适合喜欢极简风格的用户。 比如只显示分支名,分支前加个符号,分支名有颜色:

# 在 ~/.bash_profile 或 ~/.bashrc 末尾加:
# 显示 git 分支 1
parse_git_branch() {
  git branch 2>/dev/null | grep '\*' | sed 's/* //'
}
export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "
#  ✅ parse_git_branch 是一个 shell 函数,用于获取当前 git 分支名。
#  ✅ PS1 是 bash 的提示符变量,\u 是用户名,\h 是主机名,
#     \W 是当前目录,\$(parse_git_branch) 是当前 git 分支。

git1.jpg

# 在 ~/.bash_profile 或 ~/.bashrc 末尾加:
# 显示 git 分支 2
parse_git_branch() {
  git rev-parse --abbrev-ref HEAD 2>/dev/null | sed 's/\(.*\)/ (\1)/'
}
export PS1="\[\e[36m\]\u@\h \W\[\e[33m\]\$(parse_git_branch)\[\e[0m\] $ "

git2.jpg

2 使用 bash-git-prompt (推荐,丰富美观)

bash-git-prompt 是一个开源项目,支持分支、状态、颜色、符号等,配置简单,效果美观。

# 显示 git 分支 3

# 下载 + 安装(保存到 ~/.bash-git-prompt 目录)
git clone https://github.com/magicmonty/bash-git-prompt.git ~/.bash-git-prompt

# 打开文件
open -e ~/.bash_profile
# vim ~/.bash_profile # (命令行打开)

# 在 ~/.bash_profile 或 ~/.bashrc 末尾加:
GIT_PROMPT_ONLY_IN_REPO=1
if [ -f "$HOME/.bash-git-prompt/gitprompt.sh" ]; then
  source $HOME/.bash-git-prompt/gitprompt.sh
fi

# 让配置生效
source ~/.bash_profile

git3.jpg

# 显示 git 分支 4

# 复制默认主题 Default.bgptheme ,新建自定义主题 MySimple.bgptheme
cp ~/.bash-git-prompt/themes/Default.bgptheme ~/.bash-git-prompt/themes/MySimple.bgptheme

# 用编辑器打开 MySimple.bgptheme,在文件开头或合适位置添加:
GIT_PROMPT_START_USER="_LAST_COMMAND_INDICATOR_ ${Yellow}\W${ResetColor}"
GIT_PROMPT_END_USER=""
# _LAST_COMMAND_INDICATOR_ 就是你看到的 ✔
# \W 是当前文件夹名

# 在 ~/.bash_profile 或 ~/.bashrc 末尾加:
export GIT_PROMPT_ONLY_IN_REPO=1 
# GIT_PROMPT_ONLY_IN_REPO=1 只有在 git 仓库目录下才显示 bash-git-prompt 的 git 分支和状态提示,其他普通目录下不会显示 git 相关内容。
# export 主要用于将变量设置为“环境变量”,让它在当前 shell 及其子进程(比如你运行的脚本、程序)中都能访问。
export GIT_PROMPT_THEME=MySimple
# 使用 自定义主题
if [ -f "$HOME/.bash-git-prompt/gitprompt.sh" ]; then
  source $HOME/.bash-git-prompt/gitprompt.sh
fi


# 让配置生效
source ~/.bash_profile

git4.jpg

3 使用 oh-my-zsh(极致美观,功能强大)

如果你愿意换成 zsh(macOS 新版默认),可以用 oh-my-zsh,内置 git 插件和主题,分支显示极其美观。

# 安装 oh-my-zsh:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# 打开配置
open -e ~/.zshrc

# 选择主题(如 agnoster、powerlevel10k):
# 编辑 ~/.zshrc,设置:
export ZSH_THEME="agnoster"
# 或
export ZSH_THEME="powerlevel10k/powerlevel10k"
# 1. 查看 zsh 是否已安装
# macOS 10.15 及以上系统自带 zsh,一般都已安装。你可以用下面命令确认:
zsh --version
# zsh 5.9 (x86_64-apple-darwin22.0)

# 2. 切换默认 shell 为 zsh
chsh -s /bin/zsh $USER

# 检查 shell 是否切换成功
echo $SHELL
# 如果输出 /bin/zsh,说明已经切换成功。

输入 git 时 tab 自动提示

问题: 如何在终端中输入 git checkout ma 时,点击 tab 能自动 git checkout master

解决: 临时使用 zsh 。oh-my-zsh 默认自带 git 补全功能,只要 plugins=(git),就可以自动补全分支名。

# 1. 安装 git-completion 脚本
# macOS 自带 bash 可能没有自动补全脚本,你可以这样安装:
curl -o ~/.git-completion.bash https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
# ✅ 命令含义
# curl:是一个命令行工具,用于通过网络协议(如HTTP、HTTPS等)传输数据
# -o ~/.git-completion.bash:指定要把下载的文件保存为~/.git-completion.bash
# https://...:目标文件的URL,是GitHub仓库中的Git项目的补全脚本文件

# 输出如下:
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
 72 94587   72 68331    0     0   2362      0  0:00:40  0:00:28  0:00:12     0
# ✅ 解释如下:
# % Total72总体完成百分比(72%)
# % Received72接收数据的完成百分比
# Xferd72已传输的数据百分比
# Average Speed2362平均下载速度(字节/秒)
# Time Spent0:00:28已经花费的时间
# Time Left0:00:12预计剩余时间
# Current Speed0当前下载速度(字节/秒)
# Dload94587已下载的总字节数
# Upload0上传的字节数(本次为纯下载操作)

# 2. 在 ~/.bash_profile 或 ~/.bashrc 里添加:
   if [ -f ~/.git-completion.bash ]; then
     source ~/.git-completion.bash
   fi

# 生效
   source ~/.bash_profile
❌
❌