git reset文件和git checkout文件有什么区别? [英] What's the difference between git reset file and git checkout file?

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

问题描述

为什么git允许我重置文件?我认为我理解 reset ,因为它正在移动HEAD ......显然我错了。

所以, git reset sha file 好像和 git checkout sha file 一样,除了我看到文件在索引和工作目录中。



这对我没有意义。有人可以解释一下这个区别吗?

解决方案

git reset - soft - hard - 混合(和 - 保持 - 合并



http://git-scm.com/docs/git-reset



- 混合是默认值,当您执行 git reset sha file 您正在做 mixed 重置:


- 混合



重置索引而不是工作树(即保存已更改的文件
但未标记为提交)并报告未包含
的内容更新。这是默认行为。


就像上面说的那样,这种情况下的重置根本不会碰到你的工作树,只有索引中的版本重置为沙中的版本。



git checkout 另一方面:


当给出--patch时,git checkout不会切换
分支。它从
索引文件或命名(通常为提交)更新工作树中的命名路径。

因此,当你执行 git checkout 时,你将丢失文件中的更改,并且它将被替换为sha中文件版本中的任何内容,而当你执行混合重置,只有您的索引将被重置,您的工作目录仍然会有变化,您可以稍后再次根据需要进行更改。


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?

解决方案

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 do git reset sha file you are doing mixed reset whereby:

--mixed

Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.

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:

When or --patch 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 (most often a commit).

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.

这篇关于git reset文件和git checkout文件有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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