Is there a difference between git reset --hard HEAD and git checkout .?

萝らか妹 提交于 2019-12-03 04:18:56

问题


If I make changes to the working tree and have not yet committed, and I would like to revert the changes I have made, is there a difference between

git reset --hard HEAD

and

git checkout .

?


回答1:


git checkout -- . will obviously only work on the current directory (and subdirectories thereof), git reset --hard will operate on the complete working tree.

git checkout -- . will only update the working tree and leave already staged files as is, whereas git reset --hard will match index and working tree with the HEAD commit.

when used with a refspec:

  1. reset will set the current branch head to the given commit (and matches index and working tree)
  2. checkout will switch to that branch, leaving local changes intact, when they touch files which did not change between the current branch and the branch to be checked out



回答2:


These answers are good. I'd like to add that if you have deleted files, which are staged but not yet committed, then a git checkout . alone will not bring the deleted files back into the workspace. A git reset --hard will.



来源:https://stackoverflow.com/questions/5128806/is-there-a-difference-between-git-reset-hard-head-and-git-checkout

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