20

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 .

?

bsamek
  • 1,773
  • 3
  • 17
  • 23
  • 1
    possible duplicate of [Is there a difference between "git reset --hard hash" and "git checkout hash"?](http://stackoverflow.com/questions/2541545/is-there-a-difference-between-git-reset-hard-hash-and-git-checkout-hash) – Casebash Nov 16 '11 at 05:52
  • This is a nice (and thorough) explanation by the author of the Pro Git book: https://git-scm.com/book/en/v2/Git-Tools-Reset-Demystified – dubiousjim Sep 24 '12 at 10:47

2 Answers2

30

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
knittl
  • 246,190
  • 53
  • 318
  • 364
  • git checkout -- . seems to be clearing out the staging area as well. I am not sure about the first point as I always work out of the root directory. – Moondra Dec 14 '19 at 22:55
  • @Moondra I just tried again with Git 2.24 and I cannot confirm your statement. `git checkout` leaves the staging area untouched, so already-staged files will remain staged. – knittl Dec 15 '19 at 07:21
  • Actually it seems `git checkout HEAD -- .` clears the staging area. I actually made a separate thread on it as I thought they were the same. https://stackoverflow.com/questions/59339986/is-there-a-difference-between-git-checkout-head-and-git-reset-hard-head/59340544#59340544 But you are right. I was using HEAD. Sorry – Moondra Dec 15 '19 at 19:37
5

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.

Ajay
  • 18,086
  • 12
  • 59
  • 105
Homan
  • 25,618
  • 22
  • 70
  • 107