【Git 学习笔记_24】Git 使用冷门操作技巧(三)——彩色命令行 + Tab 自动补全 + 自定义状态
文章目录
- 11.5 设置彩色命令行界面
- 11.6 自动补全
- 11.7 让 Bash 自带状态信息
本节所在位置:
- 活用 git stash(一)
- 保存并应用 stash(一)
- 用 git bisect 进行调试(二)
- 使用 git blame 命令(二)
- 设置彩色命令行界面(三) ✔️
- 自动补全(三) ✔️
- 让 Bash 自带状态信息(三) ✔️
- 更多别名
- 交互式新增提交
- 用 Git 的图形界面交互式新增
- 忽略文件
- 展示与清理忽略文件
11.5 设置彩色命令行界面
默认的 git 命令行内容是黑白模式的,切换彩色模式只需一步简单配置:
$ git config --global color.ui true
设置生效前后效果对比演示:
$ git clone https://github.com/PacktPublishing/Git-Version-Control-Cookbook-Second-Edition_tips_and_tricks.git colorUI
$ cd colorUI
$ echo "And another line" >> foo
$ git add foo
$ echo "Last line ...so far" >> foo
$ touch test
# before configuration
$ git status
# set color UI
$ git config --global color.ui true
# check status by comparison
$ git status
配置前:
配置后:
彩色输出结果对多种 git
命令生效,如 diff
、log
、branch
等:
$ git log --oneline --decorate --graph
实测效果:
11.6 自动补全
git
自动补全,是指键入 Tab 键自动补全相关命令或参数项。通常在安装 git
时自带该功能。关键是将自动补全配置文件 git-completion.bash
放入指定路径:
操作系统 | git-completion.bash 路径 |
---|---|
Linux | /etc/bash_completion.d/ 下 |
MacOS | /Library/Developer/CommandLineTools/usr/share/git-core/ 下 |
Mac from Homebrew | /usr/local/Cellar/git/2.15.0/etc/bash_completion.d/git-completion.bash |
Windows | %GIT_HOME%\mingw64\share\git\completion |
如果文件缺失,可从 GitHub
下载:https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
激活配置脚本:
# take Linux for instance
if [ -f /etc/bash_completion.d/git-completion.bash ]; thensource /etc/bash_completion.d/git-completion.bash
fi
自动补全不仅对命令生效(如 git che<tab><tab>
),还可以用于提示命令参数:(如 git branch --<tab><tab>
):
11.7 让 Bash 自带状态信息
本节示例需在 Linux
环境运行:
root@SinosoftE14-Zad:/mnt/c/Users/z/Desktop/colorUI# echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
# \u: user
# \h:hostname
# \w:the current working directory relative to the user's home directory# add a branch name after the working directory
root@SinosoftE14-Zad:/mnt/c/Users/z/Desktop/colorUI# PS1='\u@\h:\w$(__git_ps1 " (%s)") \$ '
root@SinosoftE14-Zad:/mnt/c/Users/z/Desktop/colorUI (master) # pwd
/mnt/c/Users/z/Desktop/colorUI
root@SinosoftE14-Zad:/mnt/c/Users/z/Desktop/colorUI (master) #
由于当前目录在上一节的 git
库 master
分支,因此命令行的提示符多了 (master)
字样。
其他可供配置的环境变量:
参数 | 参数值 | 配置说明 |
---|---|---|
GIT_PS1_SHOWDIRTYSTATE | Nonempty | 显示 * :表示未暂存的变更;+ :表示已暂存的变更 |
GIT_PS1_SHOWSTASHSTATE | Nonempty | 如果有内容被临时存储(stashed),则显示字符 $ |
GIT_PS1_SHOWUNTRACKEDFILES | Nonempty | 若仓库文件未进入版本管理,则显示字符 % |
GIT_PS1_SHOWUPSTREAM | auto verbose name legacy Git svn | auto :当前是否落后于(< )或领先于(> )上游分支。如果分支已分叉,则显示 <> ,如果是最新的,则显示 = 。verbose 显示落后/领先的提交版本数。name 显示上游 upstream 名称。legacy 是旧版 Git 中的详情信息。Git 表示将 HEAD 与 @{upstream} 进行比较。svn 表示将 HEAD 与 svn upstream 进行比较。 |
GIT_PS1_DESCRIBE_STYLE | contains branch describe default | 在分离的 HEAD 上显示额外信息。contains 相对于一个更新的注释标签 (v1.6.3.2~35 )。branch 相对于一个更新的标签或分支 (master~4 )。describe 相对于一个较旧的注释标签 (v1.6.3.1-13-gdd42c2f )。default 表示完全匹配标签。 |
由于 Windows
版 Git
已配置了 oh-my-posh
主题样式,故 Windows
不考虑按本书定制命令行。
在 ~/.bashrc
设置如下内容:
export GIT_PS1_SHOWUPSTREAM=auto
export GIT_PS1_SHOWDIRTYSTATE=enabled
PS1='u@h:w$(__git_ps1 " (%s)") $ '
测试效果:
$ cd bugHunting
$ touch test
$ git add test
$ echo "Testing" > test
$ git commit -m 'test'
有了定制化的配置项 __git_ps1
,合并、变基、二分查找相关命令都会在命令行中通过符号予以实时展示,git status
命令瞬间黯然失色。
发散:设置彩色命令行
现如今不支持彩色显示的命令行还叫命令行吗?在 ~/.bashrc
加入如下配置项:
export GIT_PS1_SHOWUPSTREAM=auto
export GIT_PS1_SHOWDIRTYSTATE=enabled
export GIT_PS1_SHOWCOLORHINTS=enabled
PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\$ "'
看看效果: