How to use Git elegant version rollback it?

Introduction

In the version of the iterative development process, I believe many people will have been submitted when the error (at least a good many have had several such experiences). In this case, novice programmers might drive the tiger startled, tension was at a loss. The senior programmer will smile, touch the shiny forehead, then silently for version rollback.

For the version of the rollback, we often use the two commands :

  1. git reset
  2. git revert

That these two commands What difference does it make? To not worry, we detail later.

git reset

If our system now has several submitted as follows:

How to use Git elegant version rollback it?  How to use Git elegant version rollback it?
How elegant Git for version rollback?

Wherein: A and B are submitted to normal, while C and D are the dates. Now, we want to return back to C and D. By this time, HEAD pointer to submit D (5lk4er). We just need to move the pointer to B HEAD submission (a0fvf8), can achieve their goals.

As long as there git-based friends will think git reset command. Complete command as follows:

git reset --hard a0fvf8

After the command to run, HEAD pointer will move to the next submitted B, illustrated as follows:

How to use Git elegant version rollback it?  How to use Git elegant version rollback it?
How elegant Git for version rollback?

And this time, HEAD pointer remote repository remains unchanged, still on the D submitted. So, if the direct use git push command, you will not be able to push changes to the remote repository. At this point, you can only be submitted using the -f option to force push to a remote repository:

git push -f

In this way the fallback code obvious drawbacks that make HEAD pointer is moved back, so that after the submission of information will be lost. If you suddenly find that in the future, C and D are what a wonderful idea, but they had already disappeared in the long river of history.

Moreover, some companies (such as Xu Liang's company) banned the use git reset command to roll back the code, the same reasons described above. So, we need to find a command, either rollback the code, and keep the offending submitted. At this time, git revert command comes in handy.

git revert

git revert role by doing anti create a new version, this version of the content and we retreated to return to the target version of the same, but HEAD pointer is a pointer to the new generation version, rather than the target version.

Use git revert command to achieve the above example, we can do this: first revert D, and then revert C (there are multiple submissions requires a rollback if need be by newest revert):

git revert 5lk4er
git revert 76sdeb

Here you will generate two new, submitted: D 'and C', illustrated as follows:

How to use Git elegant version rollback it?  How to use Git elegant version rollback it?
How elegant Git for version rollback?

There are only two submitted need revert, we can roll back one by one. But if there are dozens of it? One fallback certainly inefficient and error-prone. We can use the following methods batch rollback:

git revert OLDER_COMMIT^..NEWER_COMMIT

In this case, the error submit C and D remain, and time in the future also be thrown pot according to follow. Further, this operation HEAD pointer is then moved back, can be directly pushed to the remote command git push warehouse. And this approach, it is encouraged by the enterprise.

Examples of us a little more difficult to give another.

If there are three submission, but very unfortunately, that is just wrong to submit in the middle. Illustrated as follows:

How to use Git elegant version rollback it?  How to use Git elegant version rollback it?
How elegant Git for version rollback?

In this case, the direct use git reset command resets the HEAD pointer to A, as is clearly not, as is the right to submit C, you need to be retained. B and C should first submit submit all the rollback, then use the cherry-pick command to re-generate C submit a new submission C '', so to achieve the B Commits a rollback needs. The complete process is as follows:

How to use Git elegant version rollback it?  How to use Git elegant version rollback it?
How elegant Git for version rollback?

Can be found in the above comparison, git reset and git revert biggest difference is that, git reset will lose the back of the submission, and git revert to re-create a new submission by way of counter-doing, and retain the original submission. In business, we should try to use git revert command can not try to do git reset command.

Guess you like

Origin www.cnblogs.com/linuxprobe-sarah/p/11259147.html