git notes

[背景介绍]

  git一般在生成一个commit之后,如果需要修改到commit里面的commit message的话,一般会使得commit tree本身发生变化。如果只是修改commit tree的HEAD还好,如果是修改较前的一个commit,会导致后面的commit tree全都会乱掉。我们就需要有一种方法来保证:修改commit的commit message时,不需要动到commit tree。这就是我们这里要介绍到的git notes。

[用法]

  1. 为一个commit添加notes

    1.1. git notes [--ref $NOTE-REF] add -m "$MSG" $COMMIT

    1.2. 如果commit已经有notes了,这样就会abort,提示已经存在notes,那么我们就可以加上-f参数,强制覆盖notes。

      e.g. git notes add -f -m "$MSG"

      -f 的用法就是force,强制盖掉原本的notes。

    1.3. 为一个commit添加一个已经存在的notes

      git notes add [-f] -C $NOTES-ID

    1.4. 交互式添加notes

      git notes edit

      这会进入notes message的编辑界面,输入相应的message,然后保存退出即可。

  2. 移掉一个commit的notes

    git notes remove $COMMIT

  3. 将commitA的notes copy到commitB

    git notes copy commitA commitB

    这样的用法相当于 git notes add [-f] -C $(git notes list $commitA) $commitB

  4. 显示一个commit的notes

    4.1.显示notes的内容

      git notes show $COMMIT

    4.2. 显示notes的ID

      git notes list $COMMIT

  5. 显示一个notes对应的commit

    git notes list | grep $NOTE-ID

    git notes list: 如果不指定commit,那么会列举所有的notes与对应的commit的关系

[进阶知识]

  git repository里面的notes,也是采用git一贯的做法,一切皆为object,一切object都会连成tree,tree即为branch。

  git里面的branch一般被指代为.git/refs/heads/ 里面的那些branch,.git/refs/里面的其它都称之为reference,它们都具有branch的特性,所以说也是可以check-out的。

  .git/refs/notes 里面都是各个notes的tree,可以有.git/refs/notes/tree1, 也可以有.git/refs/nots/tree2。这样就有两条notes的tree。

  我们在增,删,改,查 notes的时候,--ref $NOTES-REF就指定了具体的notes的reference。

posted @ 2014-12-26 20:01  Ice_Max  阅读(1168)  评论(0编辑  收藏  举报