Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare about using checkout or reset hard to undo git commit

If I use:

$ git reset --hard HEAD~N

or

$ git checkout HEAD~N

Both of two will use the version of HEAD~N to change working directory and stage area.

If there is any different between these two commands?
Thanks.

like image 264
Wayne Chen Avatar asked Feb 22 '23 23:02

Wayne Chen


2 Answers

git checkout HEAD~N is meant to examine the commit and not to be worked on as it will create a detached head state. If you want to reset (the current branch head) to a particular commit, you use git reset [--hard]

like image 55
manojlds Avatar answered Apr 29 '23 03:04

manojlds


  • git reset --hard will reset the current branch HEAD to the specified refspec
  • git checkout will switch branch, and leave you in a detached head mode.

So with the first, you can start committing right away, in your current branch.
With the second, you need to define a branch first, you (previously) current branch HEAD hasn't move. Only the working directory has been changed. And you are no longer in any branch (hence the "detached mode").

like image 26
VonC Avatar answered Apr 29 '23 05:04

VonC