Generally, we submit code in the process of GIT add, GIT commit – M and git push. Add to the temporary storage area, submit to git library to generate version number, and push to remote warehouse for others to use. This is a complete and very smooth process. However, the actual development is not so smooth, there will always be some problems.
Git reset is the command used to back off when we submit the wrong content.
Git reset version number is to go back to the version number.
Usually we use
Git reset head is to go back to the current version. Git reset head ^ back to previous version
For example, we git add a file
At this time, we find that the wrong content has been added. At this time, we just do the add operation, that is, the modified content has been added to the temporary storage area, and the commit has not been executed, so the version number has not been generated. The content corresponding to the current version number is still the content before you add. Therefore, we only need to return the code to the current version.
Git reset head, there are also prompts in the screenshot. Use git reset head < File > to unstage. My understanding is to remove the content added to the temporary storage area.
After executing the command, let’s look at the current git status, as shown in the following figure:
Check the status and find that the status is the same as when we have modified the content of the workspace and have not added.
This indicates that git reset head only works on the temporary storage area, not on the contents of the workspace. So if we want to modify the contents of the workspace, we have to continue to execute the command to restore. According to the above screenshot, GIT add is added, because we found an error, we will not be able to execute this.
There is also a command prompt: git checkout — < File > to discard changes in working directory, which means downloading a file and discarding its changes in the workspace. Let’s just translate. Even if the file is restored, the workspace changes are gone. We execute the command to view the status, as shown in the following figure:
At this point, if you go to see the work file, the modified place will be gone, and the workspace file will be clean, which is a complete restoration of the file.
In the above scenario, there is no commit after add. Let’s take a look at what happens after add and commit
As shown in the figure, I have committed, but there is no push. We don’t care about the content of push. The push command has nothing to do with submission. It’s just pushed to the remote. If it’s pushed, it’s just that we’ll push it again after we’ve backed off. So please don’t worry about the push operation. He has nothing to do with the submitted version.
Back to the topic. We have committed, indicating that the latest version number has been generated. At this time, if we want to go back, we must go back to the previous version. If you know the version number of the previous version, GIT reset version number, it is OK. But generally, we will not remember the version number. Of course, you can execute the GIT log command to find out. Git provides us with a more simple method of backing back to the previous version, GIT reset head ^. This command is specially used to go back to the previous version. If you have gone a long way in error, you may not be able to solve it by just backing back the previous version. Then you need to look up the log and find the corresponding version number for git reset version number. After executing git reset head ^, check the status, as shown in the following figure:
This screenshot is consistent with the screenshot after only adding and no commit. The only difference is,
Git reset is followed by head and head ^. This should be well understood, because one has not committed and has not generated a version number. Another has already committed, and there are some new version numbers. To go back, you must use the previous version number.
Originally I wanted to say the difference between git reset hard and no hard, but it turned out that there was so much useless. Alas, I hope there is no dizziness. Let’s explain the function of hard
Similarly, we first add without commit. After add, we execute git reset — hard head, as shown in the following figure:
Then we execute git status to view the status, as shown in the following figure:
Note that there is a difference between this and no — hard. With the parameter — hard, the contents of the workspace are directly modified. When the — hard is not added, only the temporary storage area is operated, which does not affect the workspace. The – hard is in place in one step, and without the hard, it needs to be executed separately and in two steps. It seems that hard is a lot more convenient, but I don’t recommend you to use this one, because it has a little strong lethality. In case of hand mutilation deleted some do not want to delete, there is no regret medicine. So use it with caution
Add — hard, there is no distinction between commit and hard, no screenshot here. You can try it yourself.
This article on the detailed explanation of the difference between git reset and hard is introduced here. For more relevant git reset — hard 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!