Git reset — hard, git reset — sort and git revert

Posted by

In git operations, sometimes a wrong submission is made, but there is no push to the remote branch. To revoke the submission, you can use the git reset – soft / hard command.

Difference between the two

Git reset-soft: Returns to a version, returns only commit information, and does not return to the index file level. If you have to submit, commit directly.
Git reset – – hard: Return to a version completely, the local source code will become the content of the previous version, and the changes contained in the revoked commit will be eliminated;

The usage is as follows

Use the git log command to view all local commits

Recently, there have been three commits, one is to create a file, write a file, then modify and add my name, commit comment is my name, the third modification, plus my age, commit comment is my age.

Use git cmd git reset –hard head^

Revoke the change that I added my age and execute the git log cmd. You can see that the commit of add my age has been revoked. Open the file to see, The content was also revoked.

Let’s use another way, git reset –soft head^

Looking at it with git log, it is found that the operation of the commit of add my age has also been revoked, but open the file to view.The revised contents are still there.

Revert

Given one or more existing commits, revert the changes that the related patches introduce, and record some new commits that record them. This requires your working tree to be clean (no modifications from the HEAD commit).

Compared with git reset, it does not change the current submission history. Therefore, git revert can be used on public branches, and git reset should be used on private branches.
You can also use git revert as a way to undo committed changes, while git reset HEAD is used to undo uncommitted changes.