git Reset 文件和 git checkout 文件有什么区别?
为什么 git 允许我重置文件?我以为我理解了重置
,从某种意义上说,它正在移动头部……显然我错了。
因此,git Reset sha file
似乎与git checkout sha file
做相同的事情,除了我在索引和中看到file
工作目录。
这对我来说没有意义。有人可以解释一下区别吗?
Why is it that git allows me to reset a file? I thought I understood reset
, in the sense that it was moving the HEAD ... clearly I was wrong.
So, git reset sha file
seems to do the same as git checkout sha file
, with the exception that I see file
in the index and in the working directory.
It doesn't make sense to me. Can someone please explain the difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
tl;dr
git reset COMMIT FILE
仅更改索引,git checkout COMMIT FILE
将更改索引和工作树。git reset
有非常重要的标志--soft
、--hard
和--mixed
(和>--keep
和--merge
)http://git-scm.com/docs/git-reset
--mixed
是默认值,当您执行git reset sha file
时您正在进行混合
重置,其中:就像上面所说的那样,这种情况下的重置根本不会影响你的工作树,只有索引中的版本会重置为 sha 中的版本。
另一方面,
git checkout
:因此,当您执行 git checkout 时,您将丢失文件中的更改,并且它将被 sha 中文件版本中的任何内容替换,而当您执行混合重置时,只有您的索引会被替换。被重置,您的工作目录仍将有更改,您可以稍后根据需要再次进行更改。
tl;dr
git reset COMMIT FILE
changes only index,git checkout COMMIT FILE
will change both index and working tree.git reset
has very important flags of--soft
,--hard
and--mixed
( and--keep
and--merge
)http://git-scm.com/docs/git-reset
--mixed
is the default and when you dogit reset sha file
you are doingmixed
reset whereby:Like it says above, the reset in this case would not touch your working tree at all and only the version in the index is reset to the one in the sha.
git checkout
on the other hand:So when you do
git checkout
you will lose the changes in the file and it will be replaced with whatever was there in the version of file in sha, whereas when you do the mixed reset, only your index will be reset and your working directory will still have the changes which you can later stage again as needed.恢复对文件的更改。
从暂存区域中删除文件,但保留更改。
Reverts changes to the file.
Removes the file from the staging area, but keeps the changes.