如何根据名称为 Git 个分支着色?
How can I color Git branches based on their names?
我的本地 git 存储库中有许多分支,我保留了一个特定的命名约定,这有助于我区分最近使用的和旧的分支,或者区分与 master 合并和未合并的分支。
有没有办法在不使用外部脚本的情况下根据一些基于正则表达式的规则在 git branch
的输出中为分支名称着色?
到目前为止我想出的最好办法是 运行 git branch
通过外部脚本,并创建一个别名。但是,这可能不是很便携...
git-branch
不允许你那样做
Is there a way to color branch names in the output of git branch
according to some regexp-based rules without using external scripts?
否; Git 没有提供一种方法来根据分支名称匹配的模式自定义 git branch
输出中的颜色。
编写自定义脚本
The best I've come up with so far is to run git branch
through an external script, and create an alias.
一种方法确实是编写自定义脚本。但是,请注意 git branch
is a porcelain Git command, and, as such, it shouldn't be used in scripts. Prefer the plumbing Git command git-for-each-ref
。
这是这样一个脚本的例子;根据您的需要对其进行自定义。
#!/bin/sh
# git-colorbranch.sh
if [ $# -ne 0 ]; then
printf "usage: git colorbranch\n\n"
exit 1
fi
# color definitions
color_master="3[32m"
color_feature="3[31m"
# ...
color_reset="3[m"
# pattern definitions
pattern_feature="^feature-"
# ...
git for-each-ref --format='%(refname:short)' refs/heads | \
while read ref; do
# if $ref the current branch, mark it with an asterisk
if [ "$ref" = "$(git symbolic-ref --short HEAD)" ]; then
printf "* "
else
printf " "
fi
# master branch
if [ "$ref" = "master" ]; then
printf "$color_master$ref$color_reset\n"
# feature branches
elif printf "$ref" | grep --quiet "$pattern_feature"; then
printf "$color_feature$ref$color_reset\n"
# ... other cases ...
else
printf "$ref\n"
fi
done
为它取一个别名
将脚本放在你的路径上 运行
git config --global alias.colorbranch '!sh git-colorbranch.sh'
测试
这是我在玩具仓库中得到的(在 GNU bash 中):
我的本地 git 存储库中有许多分支,我保留了一个特定的命名约定,这有助于我区分最近使用的和旧的分支,或者区分与 master 合并和未合并的分支。
有没有办法在不使用外部脚本的情况下根据一些基于正则表达式的规则在 git branch
的输出中为分支名称着色?
到目前为止我想出的最好办法是 运行 git branch
通过外部脚本,并创建一个别名。但是,这可能不是很便携...
git-branch
不允许你那样做
Is there a way to color branch names in the output of
git branch
according to some regexp-based rules without using external scripts?
否; Git 没有提供一种方法来根据分支名称匹配的模式自定义 git branch
输出中的颜色。
编写自定义脚本
The best I've come up with so far is to run
git branch
through an external script, and create an alias.
一种方法确实是编写自定义脚本。但是,请注意 git branch
is a porcelain Git command, and, as such, it shouldn't be used in scripts. Prefer the plumbing Git command git-for-each-ref
。
这是这样一个脚本的例子;根据您的需要对其进行自定义。
#!/bin/sh
# git-colorbranch.sh
if [ $# -ne 0 ]; then
printf "usage: git colorbranch\n\n"
exit 1
fi
# color definitions
color_master="3[32m"
color_feature="3[31m"
# ...
color_reset="3[m"
# pattern definitions
pattern_feature="^feature-"
# ...
git for-each-ref --format='%(refname:short)' refs/heads | \
while read ref; do
# if $ref the current branch, mark it with an asterisk
if [ "$ref" = "$(git symbolic-ref --short HEAD)" ]; then
printf "* "
else
printf " "
fi
# master branch
if [ "$ref" = "master" ]; then
printf "$color_master$ref$color_reset\n"
# feature branches
elif printf "$ref" | grep --quiet "$pattern_feature"; then
printf "$color_feature$ref$color_reset\n"
# ... other cases ...
else
printf "$ref\n"
fi
done
为它取一个别名
将脚本放在你的路径上 运行
git config --global alias.colorbranch '!sh git-colorbranch.sh'
测试
这是我在玩具仓库中得到的(在 GNU bash 中):