92

I need to revert local changes for deployments. (I'd used svn revert for this in old skool SVN days.)

And im using git reset --hard HEAD for this. (Also git fetch and git merge origin/$branch --no-ff for syncronizing with upstream branch.)

But some articles points git checkout -f for reverting changes.

What's key differences between this commands. Which way is the recommended?

Mohammed H
  • 6,880
  • 16
  • 81
  • 127
osm
  • 4,186
  • 3
  • 23
  • 24
  • 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:50

2 Answers2

74

The two of them have the exact same effect. I recommend you to choose the solution you're the more comfortable with.

But if in this particular case the effect is the same, with different values it would be completely different. Basically (there is more, see linked topics) with a reset you move the current branch and the HEAD to a specific commit but with a checkout, you only move the HEAD . For more details see below.


Resources:

On the same topic:

davnicwil
  • 28,487
  • 16
  • 107
  • 123
Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
17

Don't have the rep to comment on other answers yet, I just wanted to add that I came across a case where the two commands do NOT have the same effect. I got into a weird state so this is definitely an edge case. Here is what happened:

I was in a branch, everything clean. I checked out master git checkout master and found from git status that there were changes to existing files not staged for a commit(yes, on the code I just checked out). I tried stashing to go back to a clean state, the stash claimed to have completed but git status still was unchanged. Also tried git reset --hard HEAD. It too reported successfully completing yet the status was no different. I could not abort these weird changes.

However, git checkout -f solved this. I was able to get away from this strange state. So, in at least some ways, the two are not the same.

user1807768
  • 687
  • 1
  • 7
  • 11
  • I just had the exact same situation. There were a bunch of modified files that git reset --hard HEAD wouldn't make go away but git checkout -f did, so they're clearly not the same at some level. – Mike Wasson Jul 09 '14 at 14:42
  • 1
    In our case, it was due to file permissions changing; this fixed it http://stackoverflow.com/questions/1257592/how-do-i-remove-files-saying-old-mode-100755-new-mode-100644-from-unstaged-cha – Mike Wasson Jul 09 '14 at 14:49
  • 2
    You might also possibly get this issue if your git line-ending settings are a bit messed up and it's trying to convert those upon checkout. – Simon East May 31 '18 at 06:36