Compare using checkout or reset hard to cancel git commit

If I use:

$ git reset --hard HEAD~N 

or

 $ git checkout HEAD~N 

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

If there is any difference between the two teams?
Thanks.

+4
source share
3 answers

git checkout HEAD~N designed to study fixation and not to be processed, as it will create the state of a single head. If you want to reset (the current branch branch) to a specific commit, you use git reset [--hard]

+4
source
  • git reset --hard will reset the current HEAD branch for the specified refspec
  • git checkout will switch the branch and leave you in disconnected headunit mode.

So, first of all, you can immediately start doing the same in your current branch.
Secondly, you need to first define the branch, you (previously) the current HEAD branch did not move. Only the working directory has been changed. And you are no longer in any branch (hence the "separate mode").

+2
source

The first will change the branch to indicate the commit you specify. Commits that were ahead of this will be lost if there is no other link to them (branch or tag). There is a reflog (git reflog) that keeps a history of what was checked (by default, this holds the last cost for 90 days)

The second will switch the branch to what you specified. Since you specified a commit, not a local branch, you will not track any changes you make here. With the exception of reflog, you will lose those commits that were made after the first check, when you do a subsequent check other than the current commit or HEAD.

The third way you can β€œcancel” a job is to put a β€œ-”. after your second command. This will change your working tree to the state in which the files are in the ad that you specified. You will still have the same branch, and it will still point to the same commit. When you run "git status", you will see that all changes in your working directory will look as if you edited your files so that they look like they were in the ad that you specified. Upon making these changes, a new commit will be made, which effectively "cancels" what follows after the commit, if indicated. Now your current branch will point to this commit. This is good practice if others may depend on the commits that you really want to delete.

+2
source

Source: https://habr.com/ru/post/1389027/


All Articles