git 打印分支说明

vhipe2zx  于 2022-12-02  发布在  Git
关注(0)|答案(9)|浏览(176)

Git支持一个分支命令选项--edit-description,它声明该选项被"各种"其他命令使用。有一个命令(至少在默认情况下)不使用它,那就是git branch(用于简单地列出本地分支)。有没有办法让git branch显示分支描述(verbose选项看起来像是添加了分支的最后一次提交)?
为了清楚起见,我希望有类似以下的内容

> git branch
* master      I am the alpha and the omega
  bugfix1     This is the fix to that thing
  future      Transition to the new architecture
6mzjoqzu

6mzjoqzu1#

我确认目前无法显示git branch分支的描述(与git config相反,请参阅下面答案的最后一部分)。
This thread包括
即将到来的v1.7.9将引入分支描述,主要用于集成过程。我认为我们可以让它对那些不广泛使用请求拉/格式补丁的用户有用。在“git branch“中显示一个简短的摘要沿着分支名称会很好。
我同意,即使分支最终被您合并到主分支,并且永远不会单独离开您的存储库,也应该给予用户访问信息。
然而,如果你说“沿着......显示一个简短的摘要",那就大错特错了。
分支描述支持是为了给予用户一个地方来记录关于分支的详细解释,大小类似于你通常在提交的日志消息或一个系列的求职信中所放置的大小。
对于一个分支,没有任何方便的地方可以这样做,因为它(1)在开发过程中本质上是一个移动目标,(2)不适合标记和注解。
已经有一个很好的地方做一个简短的总结,它被称为“分支名称”。给你的分支命名就像给你的函数命名一样。
建议的修补程序“git branch --verbose-format“尚未完成。
因此script所提到的poke,仍然是(与git alias)一种可能的解决方案:

#!/bin/perl
 
$output= `git branch`;
 
foreach my $line (split(/^/, $output)) {
  my ($is_current, $name) = split(/\s+/, $line);
  my $description = `git config branch.$name.description`;
 
  $description =~ s/\s+$//;
  printf("%1s %-22s %s\n", $is_current, $name, $description);
}

Philip Oakley在评论中建议:
可以使用git config命令显示分支说明。
为了显示所有分支描述,我使用别名

brshow = config --get-regexp 'branch.*.description'

,而对于当前的HEAD我有

brshow1 = !git config --get "branch.$(git rev-parse --abbrev-ref HEAD).description".
laik7k3q

laik7k3q2#

在终端上点击这个。如果你的分支有一些描述,这将显示分支的描述。

步骤1:

vi ~/.bashrc

**步骤2:**将此

alias git-describe-branches='for line in $(git branch); do 
     description=$(git config branch.$line.description)
     if [ -n "$description" ]; then
       echo "$line     $description"
     fi
done'

步骤3:

source ~/.bashrc

步骤4:

git-describe-branches

for line in $(git branch); do 
     description=$(git config branch.$line.description)
     if [ -n "$description" ]; then
       echo "$line     $description"
     fi
done

注意事项:
1.在git工作目录中运行此命令。
1.如果您的分支有说明,则会显示说明。

r6vfmomb

r6vfmomb3#

我知道这已经很久没有被问到了,但我刚刚开始使用分支描述,并偶然发现了这个寻找显示的想法。

    • 关于bash脚本的回答**:它只打印有描述的分支。我想模仿git branch的输出,但也要有描述。
    • 关于perl脚本的回答**:如果分支名称较长,格式化将失败。如果处于分离的HEAD状态,它还会输出扭曲。

我在Python中尝试了一下,这就是我想出的方法。它解决了我对前面答案的问题。

#!/usr/bin/python
import subprocess
import sys

def git(*args):
    return subprocess.check_output(['/usr/bin/git'] + list(args)).strip()

try:
    branches = git('branch').split('\n')
except subprocess.CalledProcessError:
    sys.exit(1)

longest = len(max(branches, key=len))
for branch in branches:
    active = '*' if branch[0] == '*' else ''
    branch = branch.lstrip(' *')
    try:
        desc = git('config', 'branch.'+branch+'.description')
    except subprocess.CalledProcessError:
        print '{:2}{}'.format(active, branch)
    else:
        print '{:2}{:{}}  {}'.format(active, branch, longest, desc)

附件A

[user|host ~/git/repo((HEAD detached at origin/master))]% git bd
* (HEAD detached at origin/master)
  branch_a
  delete_this_after_a_little_while_pls_thx_bye    long branch description
  other_branch
  yay_branches_amirite                            PR under review

证物B

[user|host ~/git/repo(other_branch_name)]% git bd
  branch_a
  delete_this_after_a_little_while_pls_thx_bye    long branch description
* other_branch
  yay_branches_amirite                            PR under review

展示C

[user|host ~/non_git_repo]% git bd
fatal: Not a git repository (or any of the parent directories): .git
anauzrmj

anauzrmj4#

我从Sahil Gulati那里得到灵感,做了这个:

alias git-describe-branches='git branch | while read line; do 
     description=$(git config branch.$(echo "$line" | sed "s/\* //g").description)
     echo "$line     $description"
done'

示例:

$ git-describe-branches 
bugfixdatum     
feat32     
feat53     
feat56     
* feat57     Projektlista
feat65     
feat72     
feat75     Prevent replacing lager with empty inventering
master     
referens

这也会打印没有描述的分支,而sed命令是因为当前分支由于星号字符而需要特殊处理。

tp5buhyn

tp5buhyn5#

这是我正在使用的一个非常简单的脚本。它在一个漂亮的表格中显示所有分支,突出显示当前分支。也可以在cmd行中传递一个前缀,以便只显示分支的子集。

#!/bin/python3

from sys import argv
from prettytable import PrettyTable
from subprocess import check_output
import os
import sys

def format_table(table):
    table.field_names = ["branch", "description"]
    table.align["branch"] = "l"
    table.align["description"] = "l"

def show_branches(prefix):
    table = PrettyTable()
    format_table(table)
    G = "\033[0;32;40m" # GREEN
    N = "\033[0m" # Reset

    try:
        branches = check_output(['git', 'branch']).decode('utf')
    except:
        sys.exit(1)

    for branch in branches.splitlines():
        if prefix not in branch:
            continue

        curr_branch = '*' in branch
        branch_name = branch.strip() if not curr_branch else branch.strip().split(' ')[1]
        try:
            output = check_output(['git', 'config', f'branch.{branch_name}.description']).decode('utf')
            desc = os.linesep.join([s for s in output.splitlines() if s])
        except:
            desc = '---'
        if curr_branch:
            table.add_row([G+branch_name, desc+N])
        else:
            table.add_row([N+branch_name+N, desc+N])

    # Done
    print(table)

if __name__ == '__main__':
    prefix = argv[1] if len(argv) > 1 else ''
    show_branches(prefix)
yb3bgrhw

yb3bgrhw6#

2017年还是找不到好的解决方案。
我想到了另一个解决方案。第一行是在改变当前分支后需要的,以便更新BRANCH_DESCRIPTION文件。它只是运行一个“不做任何事情的编辑器”的edit-description工具。然后你可以简单地从文件.git/BRANCH_DESCRIPTION中获得描述。

(export GIT_EDITOR=':';git branch --edit-description); 
cat $GITREPOSITORY/.git/BRANCH_DESCRIPTION

注意事项:
这可能不是记录的/稳定的行为。

fgw7neuy

fgw7neuy7#

bash命令:

eval "git config branch.`git branch --show-current`.description"

如果有别名:

alias gs='git status && eval "git config branch.`git branch --show-current`.description"'
j13ufse2

j13ufse28#

我还从Sahil Gulati那里得到了灵感,我希望脚本中也包含一些没有描述的分支,让它看起来像通常的git branch一样漂亮,所以我添加了一些颜色和填充。
所以你可以做这样的事情。
创建脚本(例如~/git-branch-show-descriptions.sh)并添加执行权限。

branches=$(git branch --format="%(refname:short)")
current=$(git branch --show-current)
no_color='\033[0m'
green='\033[0;32m'
pad="                               "
pad_length=31

for line in $branches
    do
        description=$(git config branch.$line.description)
        padded_line="${line}${pad}"
        formatted_line="${padded_line:0:pad_length} ${description}"
        [ $line = $current ] \
            && echo -e "* ${green}${formatted_line}${no_color}" \
            || echo    "  $formatted_line"
    done

将别名添加到~/.bashrc

alias git-branch-show-descriptions=~/git-branch-show-descriptions.sh

找到它或者重新启动bash。

source ~/.bashrc

这应该会显示所有有分支的描述,以及没有分支的名称。选取的分支会以绿色显示。

djp7away

djp7away9#

在一行中检查分支说明

git config branch.<branch_name>.description

相关问题