git reset vs git reset HEAD [英] git reset vs git reset HEAD

查看:110
本文介绍了git reset vs git reset HEAD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次文件上传完毕,Git都会提供一些有用的说明,以便您暂时停用文件:

(使用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 36​​7c988,Nov. 2007,Git 1.5.4

 #在分支主机上
#要提交的更改:
#(使用git reset HEAD< file> ...停用)






torek 在评论中指出了一个实际的区别


通过指定 HEAD ,可以保证 HEAD 作为路径名。

例如,假设你ru n git reset zorg 。是 zorg 一个tree-ish,比如一个标记名,还是一个路径名, ./ zorg

Git的答案是:如果 git rev-parse 可以将它变成树ID,否则它是一个路径。

您可以写 git reset - - zorg git reset HEAD zorg 以确保git将其视为路径。


- )的更多信息a / 1192194/6309>删除命名不正确的git分支












OP skube 增加了在评论


顺便说一句,他们确实建议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 to HEAD 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 after HEAD is taken as a path name.
For instance, suppose you run git reset zorg. Is zorg a tree-ish, such as a tag name, or is it a path name, ./zorg?
Git's answer is: it's a tree-ish if git rev-parse can turn it into a tree ID, otherwise it's a path.
You can either write git reset -- zorg or git 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.e git checkout -- <file>).
It just seems inconsistent with git reset HEAD <file>.

While git reset man page clearly indicates the lack of tree-ish in git reset <tree-ish> -- <paths> means HEAD, it is not so for git 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).
While git 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 and git checkout don't use the same default, and:

  • you can represent the default tree for git reset <tree-ish> <file>: HEAD.
    Hence git reset HEAD <file>;
  • but you cannot represent the default parameter when you don't provide a tree for git checkout: it is the index.
    Hence git checkout -- file.

The -- has to be used in the git 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 the HEAD commit (the tree-ish) to the index and then on to the working dir.

这篇关于git reset vs git reset HEAD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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