How to perform version rollback gracefully

In the iterative development process of the version, I believe that many people will have the wrong submission time (at least Liang Xu has had this experience several times). In this case, a rookie programmer may be shocked and nervous. The senior programmers will smile slightly, touch their bright foreheads, and then silently roll back the version.

For version rollback, we often use two commands:

  1. git reset
  2. git revert

What is the difference between these two commands? Don't worry, we will introduce in detail later.

git reset

If our system now has the following submissions:

Among them: A and B are normal submissions, and C and D are incorrect submissions. Now, we want to roll back C and D. At this time, the HEAD pointer points to the D submission (5lk4er). We only need to move the HEAD pointer to the B submission (a0fvf8) to achieve the goal.

As long as there git-based friends, I will think of git resetthe command. The complete command is as follows:

git reset --hard a0fvf8

After the command is executed, the HEAD pointer will move to B commit, as shown below:

At this time, the HEAD pointer of the remote warehouse remains unchanged and is still on the D submission. So, if the direct use of git pushthe command, you will not be able to push changes to the remote repository. At this point, only -fthe option to submit forcibly pushed to the remote repository:

git push -f

The disadvantage of using this method to roll back the code is obvious, that is, it will make the HEAD pointer move back, which will lose the subsequent commit information. In the future, if you suddenly discover what a wonderful idea C and D are, they will have long since disappeared in the long river of history.

Moreover, some companies (such as Liangxu's company) explicitly prohibit the use of the git reset command to roll back the code for the same reason as above. Therefore, we need to find a command that can both roll back the code and save the wrong submission. At this time, the git revertcommand comes in handy.

git revert

The role of git revert is to create a new version by doing the reverse. The content of this version is the same as the target version we want to roll back to, but the HEAD pointer points to this newly generated version, not the target version.

To use the git revert command to implement the above example, we can do this: revert D first, then revert C (if there are multiple commits that need to be rolled back, you need to revert from new to old):

git revert 5lk4er
git revert 76sdeb

Two new commits will be generated here: D'and C', as shown below:

There are only two submissions that need to be reverted, and we can revert one by one. But what if there are dozens? One by one is definitely too inefficient and error-prone. We can use the following methods for batch rollback:

git revert OLDER_COMMIT^..NEWER_COMMIT

At this time, the wrong submissions C and D will still be retained, and will be followed in the future when dumping the pot. Further, if this operation HEAD pointer is moved backward, and may be used as git pusha command to the remote push warehouse. And this approach is exactly what companies encourage.

Let's give a more difficult example.

Suppose there are three submissions, but unfortunately, the wrong submission happens to be in the middle. As shown below:

At this time, directly using the git reset command to reset the HEAD pointer to the A submission is obviously not enough, because the C submission is correct and needs to be retained. B and C first batch submitted all the rollback, then use the cherry-pickcommand to re-generate C submit a new submission C '', so to achieve the B Commits a rollback needs. The complete process is as follows:

Finally, recently many friends asked me for the Linux learning roadmap , so based on my experience, I spent a month staying up late in my spare time and compiled an e-book. Whether you are in an interview or self-improvement, I believe it will help you! The directory is as follows:

Give it to everyone for free, just ask you to give me a thumbs up!

Ebook | Linux development learning roadmap

I also hope that some friends can join me to make this e-book more perfect!

Gain? I hope the old irons will have a three-strike combo so that more people can read this article

Recommended reading:

Guess you like

Origin blog.csdn.net/yychuyu/article/details/108285676