What is the difference between “git reset --hard” and “git checkout .”?

后端 未结 3 1529
心在旅途
心在旅途 2021-01-19 02:47

When I want some changes in my project and I want to return to the state of the last commit, I can use these both options. Do they actually do the same thing or is there any

相关标签:
3条回答
  • 2021-01-19 03:35

    Here is the difference between the two commands:

    git checkout .
    

    This tells Git to checkout the current folder, whatever that is, from the current branch and replace the working folder with it. But this does not affect other areas of the working folder, nor does it involve the stage.

    git reset --hard
    

    This resets the entire working directory and stage to the HEAD of the current branch. You can think of this as effectively nuking everything which has happened since your last commit.

    Generally speaking hard reset is something you won't use that often, whereas checking out files/folders from various places is more common.

    0 讨论(0)
  • 2021-01-19 03:38

    There are same if you don't follow them with any commit id i.e. they resets your state to the latest commit. However if you do reset --hard <commit_id>, it changes the HEAD of your current branch to commit id specified, whereas checkout creates a temporary branch.

    git checkout 6a0ff74 
    # would create a temp branch with its HEAD pointed to commit id
    # you can just checkout your original branch.
    
    git reset --head 6a0ff74
    # would change the `HEAD` of the current branch. To reverse the change
    # you must find the latest commit id and `reset --hard` to it again.
    
    0 讨论(0)
  • 2021-01-19 03:46

    git checkout get update data from Git server, also keep your change at local machine.

    git reset --hard make a copy identity like Git server at a specific HEAD, and discard all change at your local machine.

    0 讨论(0)
提交回复
热议问题