scenario

Git reset comes to mind when we want to roll back code, but rollback is not as powerful as you might think. Before and after you roll back, you need to be careful not to fall into some big hole

use

First, we often want to roll back should be the last time, can use the following command.

git reset HEAD^

What about the last time, and the last time? Good. I’ll just add the extra ^

Git reset HEAD^^

Git reset HEAD^^^

Of course, it can also be in the following form:

git reset HEAD~1

git reset HEAD~2

git reset HEAD~3

So that’s the same effect as above.

But here comes the problem, we have found a serious bug in the project, but this is a long time ago, we submitted many times, do we need to write a good ^? Or count them one by one?

Of course not. At this point, we can go to rollback via commit_id, via the command

git log

or

git relog

Look at the commit_id of each commit, find the commit_id of the location we want to roll back, and copy. Use the following command to roll back.

git reset [commit_id]

If we can’t roll back all files, what can we do? Very simple, just add the above command after we want to rollback file path can be. That is:

git reset [commit_id] [file]

Git reset is one of the three forms of git reset (default: mixed).

Git reset

  • — Mixed. Backs back a version, and restores all content in the staging area and locally committed content to an unstaging state. Original local files are not affected, and uncommitted files are not affected. That’s the nice thing about it, you don’t have to commit and then roll back. However, it is highly recommended that you commit before rolling back.
  • –sort. Reverts to a version without emptying the staging area, restores committed content to the staging area without affecting the original local files (uncommitted files are also not affected)
  • — hard. Back a version, to empty the staging area, will be submitted to a local version, the content of the local files will be restored version replace, this is why it is recommended that you must submit their first before rollback code, it is necessary (if use – hard because of you, you don’t have to submit at the same time, Really can’t go back, don’t ask me why I know)