git reset vs git reset HEAD [英] git reset vs git reset HEAD
问题描述
(使用git重置HEAD< file> ...停止)
然而,体面的 Atlassian的Git教程简单地说:
git reset< file>
这似乎更直接, div class =h2_lin>解决方案
没有区别(来自 <$ c
$ b
<$ c> git reset 手册页)
在所有表单中,c $ c>< tree-ish> /< commit> 默认为HEAD
。
最初的信息并非包含HEAD: commit 3c1eb9c,2007年1月,git 1.5.0-rc1 ,但由于默认情况下 总是已知,所以帮助消息清楚了哪个提交你应该重置。
HEAD
出现在 commit 367c988,Nov. 2007,Git 1.5.4 :
#在分支主机上
#要提交的更改:
#(使用git reset HEAD< file> ...停用)
torek 在评论中指出了一个实际的区别 :
通过指定
HEAD
,可以保证HEAD
作为路径名。
例如,假设你ru ngit reset zorg
。是zorg
一个tree-ish,比如一个标记名,还是一个路径名,./ zorg
?
Git的答案是:如果git rev-parse
可以将它变成树ID,否则它是一个路径。
您可以写git reset - - zorg
或git reset HEAD zorg
以确保git将其视为路径。
在 - )的更多信息a / 1192194/6309>删除命名不正确的git分支。
顺便说一句,他们确实建议disca工作目录中的rding变化
(即git checkout - < file>
)。
这似乎与git reset HEAD< file>
。
git reset
手册页清楚地表明缺少树型在 git reset< tree-ish> - <路径>
表示HEAD,但对于 git checkout< tree-ish> - < paths>
。
git checkout< tree-ish> - < pathspec>
当
<路径>
给出,git checkout
不会切换分支。
它从索引文件更新工作树中的命名路径或从< tree-ish> (通常是提交)。 :
这意味着
git checkout - path
将覆盖已经执行的工作树(git add
与HEAD包含的内容(实际上不加载添加的内容)
git reset
和git checkout
不要使用相同的默认值,并且:
- 您可以表示默认值树用于
git reset< tree-ish> < file>
HEAD
。
因此git reset HEAD< file> code>;
,但是当您不为 git checkout
提供树时,您无法表示默认参数:it是索引。
因此git checkout - file
。
在
git checkout
情况下必须使用-
,因为只有一个参数,并且需要清楚的是参数表示文件。
请注意,
git checkout HEAD文件
不同: torek 提及 $ b
<$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'code> git checkout HEAD路径将副本从
HEAD
提交(tree-ish)到索引,然后转到工作目录。
Every time a file has been staged, Git offers helpful instructions in the event you needed to unstage a file:
(use "git reset HEAD <file>..." to unstage)
However the decent Git Tutorials by Atlassian simply say:
git reset <file>
This seems more straightforward, so why the difference?
解决方案No difference (from
git reset
man page) in term of default parameter:The
<tree-ish>/<commit>
defaults toHEAD
in all forms.That message initially did not include HEAD: commit 3c1eb9c, Jan. 2007, git 1.5.0-rc1, but since the default is not always known, the help message makes it clear to which commit you are supposed to reset.
HEAD
appears in commit 367c988, Nov. 2007, Git 1.5.4:# On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage)
torek points out an actual difference in the comments:
By specifying
HEAD
, you guarantee that the first word afterHEAD
is taken as a path name.
For instance, suppose you rungit reset zorg
. Iszorg
a tree-ish, such as a tag name, or is it a path name,./zorg
?
Git's answer is: it's a tree-ish ifgit rev-parse
can turn it into a tree ID, otherwise it's a path.
You can either writegit reset -- zorg
orgit reset HEAD zorg
to make sure that git treats it as a path.See more on the double hyphen syntax (
--
) in "Deleting a badly named git branch".
The OP skube adds in the comments:
As an aside, they do suggest it for discarding changes in working directory
(i.egit checkout -- <file>
).
It just seems inconsistent withgit reset HEAD <file>
.While
git reset
man page clearly indicates the lack of tree-ish ingit reset <tree-ish> -- <paths>
means HEAD, it is not so forgit checkout <tree-ish> -- <paths>
.git checkout <tree-ish> -- <pathspec>
When
<paths>
are given,git checkout
does not switch branches.
It updates the named paths in the working tree from the index file or from a named<tree-ish>
(most often a commit).That means
git checkout -- path
will override the working tree with what has already been staged (git add
'ed).
Whilegit reset -- PATH
(being the mixed form of git reset) will reset the index with what HEAD contains (effectively un-staging what was added)
git reset
andgit checkout
don't use the same default, and:
- you can represent the default tree for
git reset <tree-ish> <file>
:HEAD
.
Hencegit reset HEAD <file>
;- but you cannot represent the default parameter when you don't provide a tree for
git checkout
: it is the index.
Hencegit checkout -- file
.The
--
has to be used in thegit checkout
case, since there is only one parameter, and it needs to be clear that parameter represents files.Note that
git checkout HEAD files
is different: torek mentions in the comments
git checkout HEAD path
copies from theHEAD
commit (the tree-ish) to the index and then on to the working dir.
这篇关于git reset vs git reset HEAD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!