What's the difference between git reset file and git checkout file?

限于喜欢 提交于 2019-12-05 00:22:30
manojlds

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 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 checkout

Reverts changes to the file.

git reset

Removes the file from the staging area, but keeps the changes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!