What is difference between "git checkout -f" and "git reset --hard HEAD"?
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?
Solution 1:
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:
- git reset
- git checkout
On the same topic:
- Is there a difference between git reset --hard HEAD and git checkout .?
- Is there a difference between “git reset --hard hash” and “git checkout hash”?
- Can you explain to me git reset in plain english?
Solution 2:
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.