Git中是否有一种为分支提供“描述”的方法?
虽然我尝试使用描述性的名称,但在一个分支上工作一段时间,有时会使我对为什么要创建其他一些主题分支的记忆变得模糊。我尝试为分支使用描述性的名称,但我认为“描述”(关于分支目的的简短说明)会很好。
Git中是否有一种为分支提供“描述”的方法?
虽然我尝试使用描述性的名称,但在一个分支上工作一段时间,有时会使我对为什么要创建其他一些主题分支的记忆变得模糊。我尝试为分支使用描述性的名称,但我认为“描述”(关于分支目的的简短说明)会很好。
当前回答
你可以使用
git config --get-regexp "branch.*.description"
其他回答
如果您最终要使用README,请创建一个git别名,修改git checkout,以便在每次切换分支时显示您的README。
例如,在~/中添加这个。Gitconfig,在[alias]下
cor = !sh -c 'git checkout $1 && cat README' -
在此之后,您可以运行git cor <branch_name>来切换分支,并显示您要切换到的分支的README。
假设您想创建一个分支
git branch branch-20200328
git notes add branch-20200328 -m "This branch is for whatever"
git notes show branch-20200328
你可以给标签添加注释:
git tag -m 'this was a very good commit' tag1
按照惯例,您可以使用与分支名称相关的标记,或者您可以使用标记-f在主题分支的头部保留一个注释标记。
你可以使用
git config --get-regexp "branch.*.description"
关于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}