Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Git to achieve elegant version fallback

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use Git to achieve elegant version fallback". In daily operation, I believe many people have doubts about how to use Git to achieve elegant version fallback. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the question of "how to use Git to achieve elegant version fallback"! Next, please follow the editor to study!

Introduction

In the process of version iterative development, it is believed that many people will have the time of error submission (at least a few times). In this case, rookie programmers may be so nervous that they don't know what to do. The senior programmer will smile, touch the shiny forehead, and then silently go back to the version.

For version fallback, we often use two commands:

Git reset

Git revert

So what's the difference between the two orders? There is no hurry, we will introduce it in detail later.

Git reset

Suppose our system now has the following submissions:

How does Git perform version fallback gracefully?

Where An and B are normal submissions, while C and D are error submissions. Now, we want to return C and D back. At this point, the HEAD pointer points to D commit (5lk4er). We just need to move the HEAD pointer to B commit (a0fvf8) to achieve this.

As long as you have friends who are basic in git, you will definitely think of the git reset command. The complete command is as follows:

Git reset-hard a0fvf8

After the command runs, the HEAD pointer moves to B submission, as shown below:

How does Git perform version fallback gracefully?

At this time, the HEAD pointer of the remote warehouse remains the same, still on the D submission. Therefore, if you use the git push command directly, you will not be able to push the changes to the remote warehouse. At this point, you can only use the-f option to force the submission to the remote warehouse:

Git push-f

The downside of backing back the code in this way is that it causes the HEAD pointer to move back, thus losing subsequent submission information. If you suddenly find out how wonderful ideas C and D are in the future, they will have disappeared in the long river of history.

Moreover, some companies, such as good promise companies, explicitly prohibit the use of git reset commands to return codes for the same reason. So, we need to find a command that can both bounce the code and save the error submission. At this point, the git revert command comes in handy.

Git revert

The role of git revert is to reverse to create a new version with the same content as the target version we are going to fallback to, but the HEAD pointer points to the newly generated version, not the target version.

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

Git revert 5lk4ergit revert 76sdeb

Here, two new submissions will be generated: D' and Crunch, as shown below:

How does Git perform version fallback gracefully?

There are only two submissions that require revert, and we can roll back one by one. But what if there are dozens? Fallback must be too inefficient and error-prone. We can use the following methods for batch fallback:

Git revert OLDER_ commit ^.. newer _ COMMIT

At this time, the wrong submission of C and D is still retained, and it can be followed when throwing the pot in the future. Moreover, in this way, the HEAD pointer is moved backwards and can be pushed directly to the remote warehouse using the git push command. And this kind of practice is exactly what enterprises encourage.

Let's give a more difficult example.

Suppose there are three submissions now, but unfortunately, that wrong submission is right in the middle. The following figure is shown:

How does Git perform version fallback gracefully?

At this point, it is obviously not possible to use the git reset command to reset the HEAD pointer to the A submission, because the C submission is correct and needs to be retained. First, roll back all C submission and B submission, and then use the cherry-pick command to regenerate C submission into a new submission Category submission, thus realizing the requirement of B submission fallback. The complete process is as follows:

How does Git perform version fallback gracefully?

From the above comparison, we can see that the biggest difference between git reset and git revert is that git reset loses the subsequent commit, while git revert re-creates a new commit in reverse and retains the original commit. In an enterprise, you should try to use the git revert command, and try not to use the git reset command as much as possible.

At this point, the study on "how to use Git to achieve elegant version fallback" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report