2013-08-07 108 views
2

git reset HEAD~1在这个git reset命令中〜1是什么意思?

我的印象是,〜1意味着:从HEAD开始,跟随1个链接,并将HEAD标记设置为新的提交节点。我期待

git reset HEAD~2

跟随2链接,然后设置HEAD标记。但是,如果我尝试了,我得到一个错误:

$ git reflog 
c83bbda [email protected]{0}: reset: moving to HEAD~1 
44c3540 [email protected]{1}: commit: you will be garbage soon 
c83bbda [email protected]{2}: reset: moving to HEAD~1 
aee7955 [email protected]{3}: commit: back to 4 lines 
c83bbda [email protected]{4}: reset: moving to HEAD~1 
19ec1d5 [email protected]{5}: commit: 3 lines 
c83bbda [email protected]{6}: reset: moving to HEAD~1 
a049538 [email protected]{7}: commit: added new line 
c83bbda [email protected]{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>...]' 

显然我错了,可是对于git的复位文档页面是不是在澄清这一非常有用的。那么,〜1是什么意思,为什么我需要它?

+2

在这种特殊情况下,“未知版本”部分意味着'HEAD〜2 “根本不存在。 'HEAD'根据reflog提交'c83bbda',这是你的初始提交,所以它没有父母,'HEAD ^','HEAD〜1'等根本不存在。 – torek

+2

请研究['gitrevisions(7)'手册](https://www.kernel.org/pub/software/scm/git/docs/gitrevisions.html),它解释了所有这些'〜'和'^'有趣的字符。 – kostix

+0

可能重复[有什么区别〜和^在混帐](http://stackoverflow.com/questions/14733687/whats-the-difference-between-and-in-git) – kostix

回答

5

HEAD~1是“的HEAD第一个亲本”,而HEAD~2是“的HEAD第一亲本​​的第一个亲本,等等(所以HEAD~n对于一些n为像HEAD后跟n ^符号和没有数字)。再次,所有的细节在git-rev-parse手册页。

git reset与“从HEAD向后倒数的修订”混合时要小心。 git reset将,一般来说,变化HEAD值,例如:

$ 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 

移动HEAD(和master)到其第一父节点。命名该父项的另一种方法是save^,另一种是save~1。此举完成后,虽然,现在HEAD名字是父母的修订,所以HEAD^父:

$ git reset HEAD^ 

你移回又迈进了一步,使masterHEAD现在命名相同的承诺是save~2名。这是很容易看到使用git rev-parse,它告诉你提交-ID,一些象征性的名称映射到:

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

在这一点上,你可以恢复master到保存点:

$ git reset save 

其移动HEADmaster回所保存的版本,然后你可以删除save安全,如果你喜欢:

$ git branch -d save 

请注意,您也可以使用git tag也保存一个保存点:分支和标签之间的唯一区别是“在分支上”(标签不移动,分支执行)时的新签入行为, (标签把你放在“分离HEAD”=不在分支状态,分支名称把你放在“分支”状态)。

2

git reser --hard HEAD~1删除最后1(或者你把任何其他数字)从当前分支提交,这样
git reset HEAD ...删除最后提交
git reset HEAD~1 ...删除最后2款
等上

+2

差不多:'git reset HEAD'不会删除提交。 'reset'的默认值是'reset --mixed',它重置索引但不改变工作树,并且无论如何,'HEAD'名称“现在你在哪里”,所以这也不会移动分支标签。因此,它取得的效果是撤消任何'git add's和/或'git rm --cached'的效果。 (说它*删除*提交也是夸大其辞,它通过改变分支名称的SHA-1值使得提交*出现*消失,最终通过'git gc'离开。) – torek

+0

谢谢澄清:) –

3

关于如何指定投入更多详情,请参见git help revisionsGit - Revision Selection

<rev>~<n>, e.g. master~3 

A suffix ~<n> to a revision parameter means the commit object that is the <n>th generation ancestor of the named commit object, following only the first parents. I.e. <rev>~3 is equivalent to <rev>^^^ which is equivalent to <rev>^1^1^1 .

这句法可与大多数Git命令中使用,所以它不是具体到git reset

1

当您运行git reset --hard HEAD~2

fatal: ambiguous argument 'HEAD~2': unknown revision or path not in the working tree. 

,因为你想你的工作副本重置提交不存在您收到此错误信息。根据你引用日志,你必须运行此命令时,你的初始根提交签出:

$ git reflog 
c83bbda [email protected]{0}: reset: moving to HEAD~1 
# etc ... 
c83bbda [email protected]{8}: commit (initial): first commit 

所以,根据上述引用日志,你当前的工作副本,即HEAD,是你的第一个承诺,所以通过从这里做git reset --hard head~2,你告诉混帐回去2款之前,你的第一次提交,这当然才可以这样做,因为都不存在,你的第一个承诺:

fatal: ambiguous argument 'HEAD~2': unknown revision or path not in the working tree. 
相关问题