我们如何缩写参考?

时间:2019-01-13 17:57:54

标签: git

我听说我们可以使用完整的ref名称或其缩写。

例如,在以下情况下,我们如何缩写引用ref

.git/ref
.git/refs/ref
.git/refs/tags/ref
.git/refs/heads/ref
.git/refs/remotes/ref
.git/refs/remotes/ref/HEAD

谢谢。

2 个答案:

答案 0 :(得分:2)

通常,Git中的大多数引用都位于refs/命名空间下:因此refs/heads/master是分支masterrefs/tags/v1.0.0是标签v1.0.0,并且refs/remotes/origin/master是远程masterorigin的远程跟踪分支。所有这些都是完整参考。

键入git checkout master之类的内容时,您将缩写该引用:Git将尝试将其解析到正确的位置(在这种情况下,通常是分支)。

让Git为您执行此操作的方法是使用git rev-parse --abbrev-ref。例如,在我的Git副本中,我得到以下信息:

$ git rev-parse --abbrev-ref refs/heads/master
master
$ git rev-parse --abbrev-ref refs/remotes/origin/master
origin/master
$ git rev-parse --abbrev-ref refs/tags/v1.0.0
v1.0.0
$ git rev-parse --abbrev-ref HEAD
master

顺便说一句,最后的情况是问“我在哪个分支上?”的最佳方法。

您还可以在git rev-parse --abbrev-ref中使用简称,如下所示:

$ git rev-parse --abbrev-ref v1.0.0
v1.0.0

答案 1 :(得分:1)

手册git help revisions<refname>部分告诉您:

   <refname>, e.g. master, heads/master, refs/heads/master
       A symbolic ref name. E.g.  master typically means the commit object
       referenced by refs/heads/master. If you happen to have both
       heads/master and tags/master, you can explicitly say heads/master
       to tell Git which one you mean. When ambiguous, a <refname> is
       disambiguated by taking the first match in the following rules:

        1. If $GIT_DIR/<refname> exists, that is what you mean (this is
           usually useful only for HEAD, FETCH_HEAD, ORIG_HEAD, MERGE_HEAD
           and CHERRY_PICK_HEAD);

        2. otherwise, refs/<refname> if it exists;

        3. otherwise, refs/tags/<refname> if it exists;

        4. otherwise, refs/heads/<refname> if it exists;

        5. otherwise, refs/remotes/<refname> if it exists;

        6. otherwise, refs/remotes/<refname>/HEAD if it exists.

           HEAD names the commit on which you based the changes in the
           working tree.  FETCH_HEAD records the branch which you fetched
           from a remote repository with your last git fetch invocation.
           ORIG_HEAD is created by commands that move your HEAD in a
           drastic way, to record the position of the HEAD before their
           operation, so that you can easily change the tip of the branch
           back to the state before you ran them.  MERGE_HEAD records the
           commit(s) which you are merging into your branch when you run
           git merge.  CHERRY_PICK_HEAD records the commit which you are
           cherry-picking when you run git cherry-pick.

           Note that any of the refs/* cases above may come either from
           the $GIT_DIR/refs directory or from the $GIT_DIR/packed-refs
           file. While the ref name encoding is unspecified, UTF-8 is
           preferred as some output processing may assume ref names in
           UTF-8.