这个〜1在这个git reset命令中意味着什么? [英] What does the ~1 mean in this git reset command?

查看:160
本文介绍了这个〜1在这个git reset命令中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


git reset HEAD〜1


我以为〜1的意思是:开始在HEAD处,跟随1个链接,并将HEAD标记设置为新的提交节点。我期待




git的复位HEAD〜2




按照2个链接,然后设置HEAD标签。但是,如果我尝试它,则会收到错误消息:

  $ git reflog 
c83bbda HEAD @ {0}:复位:移动到HEAD〜1个
44c3540 HEAD @ {1}:承诺:你将垃圾很快
c83bbda HEAD @ {2}:重置:移动到HEAD〜1个
aee7955 HEAD @ {3}:承诺:回至4行
c83bbda HEAD @ {4}:复位:移动到HEAD〜1个
19ec1d5 HEAD @ {5}:提交:3行
c83bbda HEAD @ {6}:复位:移动到HEAD〜1个
a049538 HEAD @ {7}:提交:添加新行
c83bbda HEAD @ {8}:提交(初始):第一承诺


$ git reset --hard HEAD〜2
致命:模棱两可的说法'HEAD〜2':未知版本或路径不在工作树中。
使用' - '分隔修订版本的路径,如下所示:
'git< command> [< revision> ...] - [< file>]'

显然我错了,但git reset的doc页面在澄清这个方面并不是很有用。所以,〜1是什么意思,为什么我需要它?

解决方案

HEAD〜1 HEAD 的第一个父项,而 HEAD〜2 是第一个父项 HEAD 的第一个父元素,等等(对于某些n,所以 HEAD〜n 就像 HEAD 后跟n ^ 符号,并且没有数字),所有的细节都在 GIT-REV-解析的手册。



在将 git reset 与修订版从 HEAD git reset 通常会改变 HEAD 的值,例如:

  $ git checkout master#现在位于master分支的顶端
$ git分支save master#将分支尖端复制到另一个标签,保管
$ git reset HEAD ^#或git reset HEAD〜1

HEAD (和 master )移至其第一个父项。命名该父项的另一种方法是 save ^ ,另一种是 save〜1 。但是,移动完成后, HEAD 现在将该修订名称命名为 HEAD ^ > parent:

  $ git reset HEAD ^ 

让你回到另一个步骤,使现在 master HEAD >>命名与 save〜2 名称相同的提交。使用 git rev-parse 可以很容易地看到,它告诉你一些符号名映射到的提交ID:

'pre> $ GIT中REV-解析保存〜2主
0f5a13497dd3da8aff8e452c8f56630f83253e79
0f5a13497dd3da8aff8e452c8f56630f83253e79

此时,您可以使用以下命令将 master 恢复到保存点:

  $ git重置保存

移动 HEAD master 回到已保存的版本,然后您可以删除 save

  $ git branch -d save 

请注意,您也可以使用 git tag 来保存保存点:分支和标签是当在分支上(标签不移动,分支执行)和签出(标签将您置于分离头部=不是分支状态时,分支上的新签入行为名称将您置于分支状态)。


git reset HEAD~1

I was under the impression that the ~1 meant: start at the HEAD, follow 1 link, and set the HEAD tag to that new commit node. I was expecting

git reset HEAD~2

to follow 2 links and then set the HEAD tag. However, if I try it, I get an error:

$ git reflog
c83bbda HEAD@{0}: reset: moving to HEAD~1
44c3540 HEAD@{1}: commit: you will be garbage soon
c83bbda HEAD@{2}: reset: moving to HEAD~1
aee7955 HEAD@{3}: commit: back to 4 lines
c83bbda HEAD@{4}: reset: moving to HEAD~1
19ec1d5 HEAD@{5}: commit: 3 lines
c83bbda HEAD@{6}: reset: moving to HEAD~1
a049538 HEAD@{7}: commit: added new line
c83bbda HEAD@{8}: commit (initial): first commit


$ git reset --hard HEAD~2
fatal: ambiguous argument 'HEAD~2': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Apparently I was mistaken, but the doc page for git reset is not very useful in clarifying this. So, what does the ~1 mean and why do I need it?

解决方案

HEAD~1 is "the first parent of HEAD", while HEAD~2 is "the first parent of the first parent of HEAD, and so on (so HEAD~n for some n is like HEAD followed by n ^ symbols and no numbers). Again, all the specifics are in the git-rev-parse manual page.

Be careful when mixing git reset with "revisions counting backwards from HEAD". git reset will, in general, change the value of HEAD, e.g.:

$ git checkout master    # now on tip of "master" branch
$ git branch save master # copy branch tip to another label, for safekeeping
$ git reset HEAD^        # or git reset HEAD~1

moves HEAD (and master) to its first parent. Another way to name that parent is save^, and yet another is save~1. After the move finishes, though, HEAD now names that parent revision, so HEAD^ names its parent:

$ git reset HEAD^

moves you back another step, so that master and HEAD now name the same commit that save~2 names. This is easy to see using git rev-parse, which tells you the commit-ID that some symbolic-name maps to:

$ git rev-parse save~2 master
0f5a13497dd3da8aff8e452c8f56630f83253e79
0f5a13497dd3da8aff8e452c8f56630f83253e79

At this point, you can restore master to the save-point with:

$ git reset save

which moves HEAD and master back to the saved revision, and then you can delete save safely if you like:

$ git branch -d save

Note that you could save a save-point with git tag too: the only difference between a branch and a tag is the behavior of new check-ins when "on a branch" (tags don't move, branches do) and check-outs (tags put you in "detached HEAD" = not-on-a-branch state, branch names put you in "on-a-branch" state).

这篇关于这个〜1在这个git reset命令中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆