Git中是否有一种方式可以为分支添加“描述”?
虽然我尝试使用描述性名称,但是在单个分支上工作一段时间后,有时会减弱我对其他主题分支创建原因的记忆。我尽量使用描述性名称来命名分支,但我认为一个“描述”(关于分支目的的简短说明)会很好。
Git中是否有一种方式可以为分支添加“描述”?
虽然我尝试使用描述性名称,但是在单个分支上工作一段时间后,有时会减弱我对其他主题分支创建原因的记忆。我尽量使用描述性名称来命名分支,但我认为一个“描述”(关于分支目的的简短说明)会很好。
Git 1.7.9支持此功能。从1.7.9发布说明中可以看到:
* "git branch --edit-description"可用于添加描述性文本以解释主题分支的内容。
你可以看到该功能是在2011年9月引入的,通过提交:6f9a332、739453a3、b7200e8:
struct branch_desc_cb {
const char *config_name;
const char *value;
};
--edit-description::
打开编辑器并编辑文本,解释该分支是用于各种其他命令(例如request-pull
)使用的。请注意,它不适用于分离头分支。git merge --log
。
request-pull
是一个脚本,用于将两个提交之间的更改总结到标准输出中,并在生成的摘要中包含给定的URL。git branch
能在列表中显示描述就好了... - Owen如果您最终使用了README,请创建一个git别名,修改 git checkout
,以便每次切换分支时都显示您的README。
例如,在〜/ .gitconfig 下添加以下内容:
cor = !sh -c 'git checkout $1 && cat README' -
在此之后,您可以运行 git cor <branch_name>
来切换分支,并显示您要切换到的分支的 README。
1.7.11-msysgit.1
)。我改用$0,一切都好了。 - shytikovsh -c
";例如,alias = "!f() { git checkout "${1}" && cat README.md; }; f"
(在这种情况下,括号和引号不必要,只是为了完整起见,以防它们对于更复杂的内容有用)。 - michael~/.gitconfig
文件中的 [alias]
下面,而别名的名称实际上(并且令人困惑地)被称为我的实际配置中的 alias
(我应该将其重命名为 cor
以保持一致)。我的实际 alias
别名是:alias = "!f() { git config --get-regexp "^alias.${1}$" ; }; f"
。用法:git alias {alias_name}
或 git alias {alias_regexp}
。类似于 bash 的 alias
命令,例如 $ alias ll
对我来说会产生 alias ll='ls -l'
;而 $ git alias br
会产生 alias.br branch -v --list
(也可以使用:$ git alias 'b.*'
)。 - michael使用 git branch --edit-description
命令来设置或编辑分支说明。
这是一个Shell函数,它可以显示与git branch
类似的分支,但是带有附加说明。
# Shows branches with descriptions
function gb() {
current=$(git rev-parse --abbrev-ref HEAD)
branches=$(git for-each-ref --format='%(refname)' refs/heads/ | sed 's|refs/heads/||')
for branch in $branches; do
desc=$(git config branch.$branch.description)
if [ $branch == $current ]; then
branch="* \033[0;32m$branch\033[0m"
else
branch=" $branch"
fi
echo -e "$branch \033[0;36m$desc\033[0m"
done
}
这里展示了 gb
的外观,为防止图片失效,将其呈现为文本:
$ gb
* logging Log order details. Waiting for clarification from business.
master
sprocket Adding sprockets to the parts list. Pending QA approval.
图像如下,可以看到颜色:
.gitattributes
文件提交到您的代码库中,因此不会出现问题。但是,当通过某些基于Web的界面合并时,例如在Azure DevOps中使用拉取请求时,这种方法似乎无法正常工作。 - Soren Bjornstad这里有两个常见的建议:
git branch --edit-description
:我们不喜欢这个方法,因为你不能推送它。也许我能记住我创建的分支都做了什么,但我的团队肯定记不住。README
文件每个分支都有一个。这在合并时很麻烦:容易产生合并冲突,并且当我们合并特性分支时,我们将从分支中提取 README
。分支之间的差异也很麻烦。我们决定创建一个孤立的 branches-readme
分支。孤立分支是具有自己独立历史的分支 - 你可能从 Github 的 gh-pages
分支了解过它们。这个孤立分支包含一个单独的 README
文件。它的内容类似于:
master:
The default branch
mojolicious:
Start using Mojolicious
branch-whatever:
Description of the whatever branch
它可以被推送并且易于合并。通过以下命令从任何分支查看README
:
git show branches-readme:README
缺点是,当您想要更新 README
时,您需要检出奇怪的孤立分支,并且随着分支的更名、到来或离开,README
不会自动更新。对我们来说这没问题。
操作方法:
git checkout --orphan branches-readme
# All the files from the old branch are marked for addition - skip that
git reset --hard
# There are no files yet - an empty branch
ls
vi README
# put in contents similar to above
git add README
git commit -m "Initial description of the branches we already have"
git push origin branches-readme
# get all your original files back
git checkout master
类似地,单个团队成员也可以创建他们自己的 branches-$user
残留分支来描述他们自己的私有分支,只要他们不将它们推送到团队即可。
通过进一步的工具化,这也可以与 git branch
的输出集成。为此,可以考虑使用 README.yaml
文件,而不是普通的 README
。
git config --global --add alias.about '!describe() { git config branch."$1".description; }; describe'
该命令将定义一个全局选项alias.about
作为shell表达式。在存储库中运行git about <branch>
,如果设置了分支的描述,则会显示分支的描述。
"!describe() { git config branch.\"$(git symbolic-ref --short -q HEAD)\".description; }; describe"
。请注意,我保持原文的意思和结构不变,并尽可能用通俗易懂的语言来翻译。 - auggit config --global --add alias.about '!describe() { git config branch."$(git symbolic-ref --short -q HEAD)".description; }; describe'
- Tom Tresansky这是一个 git
alias
,可让您在当前分支上设置和读取描述:
git config --global --add alias.about '!describe() { msg="$1"; git config branch."$(git rev-parse --abbrev-ref HEAD)".description ${msg:+"$msg"}; }; describe'
使用方法/示例:
(develop) $ git about
(develop) $ git about message
(develop) $ git about
message
(develop) $ git about "this is a new message"
(develop) $ git about
this is a new message
(develop) $ git checkout -b test_branch
Switched to a new branch 'test_branch'
(test_branch) $ git about
(test_branch) $ git about "this is the test branch"
(test_branch) $ git about
this is the test branch
(test_branch) $ git checkout -
Switched to branch 'develop'
Your branch is up to date with 'origin/develop'.
(develop) $ git about
this is a new message
特别感谢 @Felicio 的回答,让我开始了解。
假设你想创建一个分支
git branch branch-20200328
git notes add branch-20200328 -m "This branch is for whatever"
git notes show branch-20200328
以下是Greg Hewgill提到的git branches
命令的一个可能的实现:
#!/usr/bin/perl
sub clean {
map { s/^[\s\*]*\s// } @_;
map { s/\s*$// } @_;
return @_;
}
sub descr {
$_ = `git config branch.@_.description`;
s/\s*$//;
return $_;
};
sub indent {
$_ = shift;
s/^/ /mg;
return $_;
};
my @branches = clean `git branch --color=never --list`;
my %merged = map { $_ => 1 } clean `git branch --color=never --merged`;
for my $branch (@branches) {
my $asis = `git branch --list --color=always $branch`;
$asis =~ s/\s*$//;
print " $asis";
print " \033[33m(merged)\033[0m" if ($merged{$branch} and $branch ne "master");
print "\n";
print indent descr $branch;
print "\n";
print "\n";
}
您可以将评论附加到标签:
git tag -m 'this was a very good commit' tag1