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

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


当前回答

下面是一个git别名,它可以让你设置和读取当前分支的描述:

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的回答,让我开始了。

其他回答

下面是Greg Hewgill提到的git分支命令的一个可能实现:

#!/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 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 config --get-regexp "branch.*.description"

只使用:

git config branch.<branch name>.description

在该表扬的地方表扬: https://glebbahmutov.com/blog/git-branches-with-descriptions/

Chris J建议的README可以工作,只要它使用.gitattribute中定义的自定义合并驱动程序进行设置。 这样,在合并期间始终保留README的本地版本。

分支的“描述”也被称为与元数据相关联的“注释”,它是不受支持的。

至少,对于一个README文件,你可以对任何分支执行:

$ git show myBranch:README

如果你的README在你的REPO的根目录,它将从任何路径工作,因为git show使用的路径是绝对的一个从REPO的顶部目录。