Git中是否有一种为分支提供“描述”的方法?

虽然我尝试使用描述性的名称,但在一个分支上工作一段时间,有时会使我对为什么要创建其他一些主题分支的记忆变得模糊。我尝试为分支使用描述性的名称,但我认为“描述”(关于分支目的的简短说明)会很好。


当前回答

你可以使用

git config --get-regexp "branch.*.description"

其他回答

git config --global --add alias.about '!describe() { git config branch."$1".description; }; describe'

命令将定义全局选项别名。About as shell表达式。如果在存储库中运行git about <branch>,将显示分支的描述。

你可以给标签添加注释:

git tag -m 'this was a very good commit' tag1

按照惯例,您可以使用与分支名称相关的标记,或者您可以使用标记-f在主题分支的头部保留一个注释标记。

选定的答案对我来说似乎太夸张了。我倾向于维护每个分支的描述文件,这是一个正常的源代码控制文件,比如master.txt, dev.txt等,如果有一个难以处理的数字或分支,我会创建一个层次结构来更好地组织它。

使用git branch——edit-description来设置或编辑分支描述。

下面是一个shell函数,它显示与git分支类似的分支,但附加了描述。

# 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.

作为一个图像,你可以看到颜色:

这里有两个流行的建议:

git分支——edit-description:我们不喜欢这样,因为你不能推送它。也许我能记住我创建的分支是做什么的,但我的团队肯定不能。 README文件pr.分支。这是合并过程中的一个痛苦:非常容易合并冲突,当我们合并特性分支时,我们将从分支中拉入README。分支之间的差异也是一种痛苦。

我们决定创建一个孤儿分支——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

类似地,每个团队成员也可以创建他们自己的分支-$user孤儿分支,如果他们愿意,只要他们不把它们推给团队,就可以描述他们自己的私有分支。

通过进一步的工具,这也可以与git分支的输出集成。为了这个目的,可能需要一个自述书。可以考虑使用yaml文件来代替普通的README。