第2章:直接操纵引用

基础知识

Git引用常规放在<repo>/refs/中,可以有任意层次的文件夹结构。 惯例如下:

  • heads

    • 各本地分支

  • remotes

    • 各远程仓库

      • 各远程分支

  • tags

    • 各标签

  • stash - 见第3章git stash

  • replace - 见第1章git replace

  • notes - 见第1章git notes

Git特别引用直接放在<repo>下,一般包括HEAD等。

引用可以分为直接(指向对象的)引用和间接(指向其他引用的)两种。

本章所有命令都不涉及worktree。后续章节会介绍如何利用worktree来操纵对象(Lv3)。

创建直接引用

注:若引用已存在,则会覆盖

  • Lv0

    该操作非常危险,因为没有保存操作记录。

  • Lv2

    该操作会在<repo>/logs/refs/heads/br1中留下操作记录。

  • Lv3

    该操作会在<repo>/logs/refs/heads/br1中留下操作记录,原因是branch: Created from ...或者branch: Reset to ...

查看直接引用

  • Lv0

  • Lv2

  • Lv3

创建间接引用

  • Lv0

  • Lv2

查看间接引用

  • Lv0

  • Lv2

    注意以下两者的区别

  • Lv3

    Lv3命令只能看到解引用后的对象,无法看清楚间接引用本身

删除引用

  • Lv0

  • Lv2

  • Lv3

关于update-ref的特别备注

--no-deref表明修改引用本身(不论其是什么类型的) 不带--no-deref表明修改引用本身(如果其不存在或者是直接引用)或者引用的引用(如果其是间接引用)

查看历史记录

  • Lv0

  • Lv3

批量查看引用

  • Lv2

git show-ref接类似于git rev-parse的东西,而git for-each-ref接前缀:

两个都不能列出$GIT_DIR下的引用!

给定commit-ish,逆向查找引用

  • Lv1

  • Lv2

  • Lv3

添加--dirty可以在结果后面添加-dirty,特别适用于版本号。

总结

  • 添加/修改/删除

    • Lv0

      • 不推荐

    • Lv2

      • git rev-parse <object>

      • git update-ref --no-deref <ref> [-d|<new>] - 修改<ref>

      • git update-ref <ref> [-d|<new>] - 修改<ref>或者其引用的引用

      • git symbolic-ref --delete <ref>

      • git symbolic-ref <from> <to>

    • Lv3

      • git branch -f <branch> <commit-ish> - 只能操纵refs/heads/

      • git branch -D <branch> - 只能操纵refs/heads/

      • git tag -f <tag> <commit-ish> - 只能操纵refs/tags/

      • git tag -d <tag> - 只能操纵refs/tags/

  • 查看历史记录

    • Lv0

      • cat <repo>/logs/<ref>

    • Lv3

      • git reflog

  • 单独查看

    • Lv0

      • cat <repo>/<ref>

    • Lv2

      • git rev-parse <ref> - 可以接多个但是不如git show-ref好用

      • git symbolic-ref <ref>

  • 批量查看

    • Lv2

      • git show-ref [--head] [<ref>...]

      • git for-each-ref [<ref-pattern>...]

    • Lv3

      • git branch -av

      • git branch -avl <branch-pattern>

      • git tag -l <tag-pattern>

  • 给定commit-ish,逆向查找引用

    • Lv2

      • git name-rev [--tags] --all|<commit-ish>

    • Lv3

      • git describe [--all] [--always] [<commit-ish>] - 留空表示HEAD

      • git describe [--all] [--always] --dirty

扩展阅读

git-rev-parse: specifying revisions

最后更新于

这有帮助吗?