What is difference between "git checkout -f" and "git reset --hard HEAD"?

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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:

Solution 2 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionosmView Question on Stackoverflow
Solution 1 - GitColin HebertView Answer on Stackoverflow
Solution 2 - Gituser1807768View Answer on Stackoverflow