Explain the difference between git reset — hard and git reset — soft


Sometimes, an incorrect commit has been made, but the remote branch has not been pushed. If you want to undo the commit, you can use the GIT reset – – soft / hard command.

1. The difference between them is as follows

Git reset – – soft: back to a certain version, only the commit information is rolled back, and it will not be restored to the index file level. If you still need to submit, you can directly commit;
Git reset – – hard: back to a certain version completely, the local source code will also change to the content of the previous version, and the changes contained in the revoked commit will be reversed;

2. The specific usage is as follows:

Use the GIT log command to view all local commits

Now you want to cancel the last commit, that is, “otms-25838: rollback the modification in orderdto” in the above figure;

(1) Git reset — hard example:

Use the command git reset — hard 1c38838ad39b396e271cb10e2146a1d673b4a2b9 to withdraw all submissions before the release number (excluding the submission).

Note: after the above command is successfully executed, it will return to the previous version status completely, and the new changes will be lost. For some changes, but there are uncommitted files in the changed part of the folder, which may cause the directory to be non empty and delete failure. In this case, you need to handle it yourself.

(2) Example of GIT reset – – soft:

Initial status. It can be seen that the last commit of the current branch is debug the initialization page

Now you want to undo the commit, but you can’t undo the changes included in the commit. Use git reset — soft, and the execution result is

You can see that the commit is canceled, but the code change is not cancelled.

This article on the detailed explanation of GIT reset — hard and git reset — soft differences will be introduced here, more relevant git reset — hard and git reset — soft content, please search the previous articles of developeppaer or continue to browse the related articles below. I hope you can support developeppaer more in the future!