gpt4 book ai didi

git reset 与 git reset HEAD

转载 作者:IT王子 更新时间:2023-10-29 01:28:45 30 4
gpt4 key购买 nike

每次暂存文件时,如果您需要取消暂存文件,Git 都会提供有用的说明:

(use "git reset HEAD <file>..." to unstage)

不过体面Git Tutorials by Atlassian简单地说:

git reset <file>

这看起来更直接,那么为什么不同呢?

最佳答案

在默认参数方面没有区别(来自 git reset man page):

The <tree-ish>/<commit> defaults to HEAD in all forms.

该消息最初包含 HEAD:commit 3c1eb9c, Jan. 2007, git 1.5.0-rc1 ,但由于默认值并非总是已知的,因此帮助消息清楚地表明您应该重置哪个提交。

HEAD出现在 commit 367c988, Nov. 2007, Git 1.5.4 :

# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)

torek指出实际差异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.

在“Deleting a badly named git branch”中查看有关双连字符语法 (--) 的更多信息。




OP skube添加 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>.

同时 git reset man page清楚地表明 git reset <tree-ish> -- <paths> 中缺少树状结构表示 HEAD, 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).

这意味着 git checkout -- path将用已经上演的内容覆盖工作树( git add 'ed)。
同时 git reset -- PATH (作为 git reset 的混合形式)将使用 HEAD 包含的内容重置索引(有效地取消暂存添加的内容)

git resetgit checkout不要使用相同的默认值,并且:

  • 您可以表示 git reset <tree-ish> <file> 的默认树: HEAD .
    因此 git reset HEAD <file> ;
  • 但是当您不为 git checkout 提供树时,您不能表示默认参数: 是索引。
    因此 git checkout -- file .

--必须在 git checkout 中使用case,因为只有一个参数,需要明确参数代表的是文件

请注意 git checkout HEAD files不同:torek提及 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.


注意:对于 2019 年 8 月的 Git 2.23+,您可以使用 git restore 而不是

参见 the examples :

To restore a file in the index to match the version in HEAD (this is the same as using git-reset)

$ git restore --staged hello.c

手册页:

git restore --staged hello.c不指定源,仅恢复索引 ( --staged ):它这样做(默认情况下)使用 HEAD 作为源。

By default, the restore sources for working tree and the index are the index and HEAD respectively.
--source could be used to specify a commit as the restore source.

其他例子:

You can restore both the index and the working tree (this the same as using git-checkout)

$ git restore --source=HEAD --staged --worktree hello.c

or the short form which is more practical but less readable:

$ git restore -s@ -SW hello.c

git restore是一个更自然的命令名称,没有歧义。

关于git reset 与 git reset HEAD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33849704/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com