Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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?

like image 986
osm Avatar asked Jun 01 '11 13:06

osm


People also ask

What is git reset -- hard head?

The purpose of the “git reset” command is to move the current HEAD to the commit specified (in this case, the HEAD itself, one commit before HEAD and so on). So what is the “–hard” option used for? The “–hard” option is used in order to reset the files of the index (or the staging area) and of the working directory.

What is git checkout F?

Force a Checkout You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.


1 Answers

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?
like image 65
Colin Hebert Avatar answered Sep 19 '22 18:09

Colin Hebert