没有区别(从git reset手册页 http://git-scm.com/docs/git-reset)就默认参数而言:
The <tree-ish>/<commit>
默认为HEAD
以各种形式。
该消息最初是not包括头部:提交 3c1eb9c,2007 年 1 月,git 1.5.0-rc1 https://github.com/git/git/commit/3c1eb9cb2d57769f770ddc36fd6b49706608ebb7,但由于默认是not总是已知的,帮助消息清楚地表明您应该重置哪个提交。
HEAD
出现在提交 367c988,2007 年 11 月,Git 1.5.4 https://github.com/git/git/commit/367c98866c340bc9cf5cfa88c3b69f027165fc44:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
torek https://stackoverflow.com/users/1256452/torek指出实际差异在评论中 https://stackoverflow.com/questions/33849704/git-reset-vs-git-reset-head/33849726#comment55470127_33849726:
通过指定HEAD
,你保证后面的第一个单词HEAD
被视为路径名。
例如,假设您运行git reset zorg
. Is zorg
树状结构,例如标签名称,或者是路径名称,./zorg
?
Git 的答案是:这是一个树状的 ifgit rev-parse http://git-scm.com/docs/git-rev-parse可以把它变成树ID,否则就是路径。
你可以写git reset -- zorg
or git reset HEAD zorg
确保 git 将其视为路径。
查看有关双连字符语法的更多信息 (--
) in "删除命名错误的 git 分支 https://stackoverflow.com/a/1192194/6309".
The OP skube https://stackoverflow.com/users/572747/skube adds 在评论中 https://stackoverflow.com/questions/33849704/git-reset-vs-git-reset-head/33849726?noredirect=1#comment55559254_33849726:
顺便说一句,他们确实建议放弃工作目录中的更改
(i.e git checkout -- <file>
).
只是看起来不符合git reset HEAD <file>
.
While git reset手册页 http://git-scm.com/docs/git-reset清楚地表明缺乏树状结构git reset <tree-ish> -- <paths>
表示 HEAD,但情况并非如此git checkout <tree-ish> -- <paths> http://git-scm.com/docs/git-checkout.
git checkout <tree-ish> -- <pathspec>
When <paths>
给出,git checkout
不切换分支。
它从索引文件更新工作树中的命名路径或来自指定的<tree-ish>
(最常见的是提交)。
这意味着git checkout -- path
将用已经上演的内容覆盖工作树(git add
'ed).
While git reset -- PATH
(是 git reset 的混合形式)将重置索引与 HEAD 包含的内容(有效地取消暂存添加的内容)
git reset
and git checkout
不要使用相同的默认值,并且:
- 您可以代表默认树
git reset <tree-ish> <file>
: HEAD
.
Hence git reset HEAD <file>
;
- 但是当您不提供树时,您无法表示默认参数
git checkout
: 就是索引。
Hence git checkout -- file
.
The --
必须用在git checkout
情况下,由于只有一个参数,需要明确参数代表files.
注意git checkout HEAD files
是不同的:torek https://stackoverflow.com/users/1256452/torek提及在评论中 https://stackoverflow.com/questions/33849704/git-reset-vs-git-reset-head/33849726?noredirect=1#comment55559254_33849726
git checkout HEAD path
副本来自HEAD
将(树状)提交到索引,然后提交到工作目录。
注意:对于 2019 年 8 月的 Git 2.23+,您可以使用git restore https://stackoverflow.com/a/57066072/6309 instead
See 例子 https://git-scm.com/docs/git-restore#_examples:
恢复索引中的文件以匹配 HEAD 中的版本(这与使用相同git-reset
)
$ git restore --staged hello.c
手册页:
git restore --staged hello.c
不指定源,仅恢复索引(--staged https://git-scm.com/docs/git-restore#Documentation/git-restore.txt--W):它使用 HEAD 作为源来执行此操作(默认情况下)。
默认情况下,恢复sources工作树和索引分别是索引和HEAD。
--source
可用于指定一个提交作为恢复源。
其他例子:
您可以恢复索引和工作树(这与使用git-checkout
)
$ git restore --source=HEAD --staged --worktree hello.c
或者更实用但可读性较差的简短形式:
$ git restore -s@ -SW hello.c
git restore
是一个更自然的命令名称,并且没有歧义。