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

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


当前回答

关于Greg Hewgill 2012年的回答,Git 1.7.9(2012年第一季度)也包含了一些对Git分支的检查——edit-description:

参见Junio C Hamano (gitster)提交c2d17ba(2012年2月05日)。 (由Junio C Hamano—gitster—在commit d88698e中合并,2012年2月10日)

分支——edit-description:防止输入错误的分支名称

It is very easy to mistype the branch name when editing its description, e.g. $ git checkout -b my-topic master : work work work : now we are at a good point to switch working something else $ git checkout master : ah, let's write it down before we forget what we were doing $ git branch --edit-description my-tpoic The command does not notice that branch 'my-tpoic' does not exist. It is not lost (it becomes description of an unborn my-tpoic branch), but is not very useful. So detect such a case and error out to reduce the grief factor from this common mistake. This incidentally also errors out --edit-description when the HEAD points at an unborn branch (immediately after "init", or "checkout --orphan"), because at that point, you do not even have any commit that is part of your history and there is no point in describing how this particular branch is different from the branch it forked off of, which is the useful bit of information the branch description is designed to capture. We may want to special case the unborn case later, but that is outside the scope of this patch to prevent more common mistakes before 1.7.9 series gains too much widespread use.


虽然Greg Hewgill 2012年的答案是准确的,但使用git分支——edit-description可能会导致:

fatal: could not unset 'branch.main.description'

GIT_EDITOR=: git branch——edit-description(man)导致失败,这已在git 2.39 (Q4 2022)中得到纠正。

参见juno C Hamano (gitster)提交e288b3d (30 Sep 2022)。 (由Junio C Hamano—gitster—在commit 272be0d中合并,2022年10月17日)

分支:不要失败no-op——edit-desc

Imagine running "git branch --edit-description"(man) while on a branch without the branch description, and then exit the editor after emptying the edit buffer, which is the way to tell the command that you changed your mind and you do not want the description after all. The command should just happily oblige, adding no branch description for the current branch, and exit successfully. But it fails to do so: $ git init -b main $ git commit --allow-empty -m commit $ GIT_EDITOR=: git branch --edit-description fatal: could not unset 'branch.main.description' The end result is OK in that the configuration variable does not exist in the resulting repository, but we should do better. If we know we didn't have a description, and if we are asked not to have a description by the editor, we can just return doing nothing. This of course introduces TOCTOU.

(Time-of-check to time-of-use (tocou):由竞态条件引起的一类软件错误,包括对系统部分状态的检查(例如安全凭证)以及对检查结果的使用。)

如果您从另一个窗口向同一分支添加了一个分支描述,而您打开了编辑器来编辑该描述,然后退出编辑器而没有在那里写入任何内容,那么我们最终不会删除您在另一个窗口中添加的描述。 但是在这一点上,您在自己的存储库中愚弄自己,如果这很痛苦,您最好不要这样做;-)。


另外:“git branch -edit-description”(man)在一个未出生的分支上误导性地说不存在这样的分支,这已在git 2.39(2022年第四季度)中更正。

参见Rubén Justo (rjusto)提交bcfc82b (08 Oct 2022)。 (由Junio C Hamano—gitster—在commit 4050354中合并,2022年10月17日)

分支:对不存在的分支错误的描述 署名:Rubén Justo

When the repository does not yet have commits, some errors describe that there is no branch: $ git init -b first $ git branch --edit-description first error: No branch named 'first'. $ git branch --set-upstream-to=upstream fatal: branch 'first' does not exist $ git branch -c second error: refname refs/heads/first not found fatal: Branch copy failed That "first" branch is unborn but to say it doesn't exists is confusing. Options "-c" (copy) and "-m" (rename) show the same error when the origin branch doesn't exists: $ git branch -c non-existent-branch second error: refname refs/heads/non-existent-branch not found fatal: Branch copy failed $ git branch -m non-existent-branch second error: refname refs/heads/non-existent-branch not found fatal: Branch rename failed Note that "--edit-description" without an explicit argument is already considering the empty repository circumstance in its error. Also note that "-m" on the initial branch it is an allowed operation. Make the error descriptions for those branch operations with unborn or non-existent branches, more informative. This is the result of the change: $ git init -b first $ git branch --edit-description first error: No commit on branch 'first' yet. $ git branch --set-upstream-to=upstream fatal: No commit on branch 'first' yet. $ git branch -c second fatal: No commit on branch 'first' yet. $ git branch [-c/-m] non-existent-branch second fatal: No branch named 'non-existent-branch'.


在Git 2.39 (Q4 2022)中,“Git branch——edit-description @{-1}”(man)现在是一种在切换到当前分支之前编辑你所在分支的分支描述的方法。

参见Rubén Justo (rjusto)提交0dc4e5c (11 Oct 2022)。 (由Junio C Hamano—gitster—在commit c2058ea中合并,2022年10月21日)

分支:支持快捷方式,如@{-1},已完成 署名:Rubén Justo

branch command with options "edit-description", "set-upstream-to" and "unset-upstream" expects a branch name. Since ae5a6c3 (checkout: implement "@{-N}" shortcut name for N-th last branch, 2009-01-17, Git v1.6.2-rc0 -- merge), a branch can be specified using shortcuts like @{-1}. Those shortcuts need to be resolved when considering the arguments. We can modify the description of the previously checked out branch with: $ git branch --edit--description @{-1} We can modify the upstream of the previously checked out branch with: $ git branch --set-upstream-to upstream @{-1} $ git branch --unset-upstream @{-1}

其他回答

我非常确定目前不支持该特性。我认为最好的办法是创建一个描述文本文件,基本上是一个README,在包含您想要的信息的分支中。

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

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

假设您想创建一个分支

git branch branch-20200328
git notes add branch-20200328 -m "This branch is for whatever"
git notes show branch-20200328

只使用:

git config branch.<branch name>.description

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

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