In the daily coding process, it is inevitable to merge branches, roll back, submit, tag and other operations. If you still do not know how to use Git tool to roll back code, or you are always worried about errors and are afraid of losing the code, this is very dangerous. After all, the loss of code is a big matter, and the performance will be deducted more points. But Dont Worry, you are very lucky to read this article, and when you are done there will be no code missing, because I will walk you through the hand-by-hand branching that simulates the common case of rollback

------ For more information, see 🔽Copy the code

introduce

Git (pronounced /gɪt/) is an open source distributed version control system that can handle version management of projects from the very small to the very large efficiently and quickly.

------ is from Baidu BaikeCopy the code

preface

Git revert () and git reset () are two common rollback methods. Git revert () and git reset () are two common rollback methods. Git revert () and git reset () are two common rollback methods. The main branch, master, and the development branch, Develop, do the simulation

start

Reset to introduce

Reset is used when the commit you want to commit disappears completely from history

Git reset git reset git reset git reset git reset git reset git reset git reset

3. This command is most likely to be used for our main branch, as we are working with the master branch and developing functionality from Develop

4, Merge the branches from Develop to Master. If you find problems with the merged branches before launching, roll back the branches from Develop to Master

5. In plain English, cancel the develop merge

You can use reset to rollback a COMMIT in a collaborative development situation where everyone is merged to master and you can’t use reset to rollback a COMMIT because it will flush out other people’s commits

Create a branch simulation

1. Create a new project from your own Git repository and pull it locally

2. Create an index.js, write something, and submit it to the repository

3. Git log is used to query the commit

4. We migrated a Branch git branch develop from the Master branch and switched to git Checkout Develop

5. Add a new code to the Develop branch, which adds a new record of B to the Develop commit record

6. Add another code to the Develop branch

7. If you look at the latest develop and master commit records, you can see that dev leads the master branch by two commits


Note there is a problem when you do branch merge, sometimes found that although the code is not the same but when branches merge prompt code is not updated, because the current development branch a commit record is behind the target branch to be combined, the causes of this situation is reset due to abuse, So use “reset” carefully

Use reset to get a feel for it

Git merge develop git merge develop

2. In the master branch, use git log to check the commit record, find the B record, and prepare to roll back this record

Git reset 69fde2c is used for rollback. The last new C record is not found in the log. If you use git push directly, you will see the following prompt.

This is because of the local record because our rollback is behind the repository code, and this use requires a mandatory commit using Git push -f

4. The master branch is now set to commit from A and B, and the reset rollback is complete. After that, we can continue to merge the Develop branch into the master

Revert to introduce

1. The principle of revert is to add a new submission after the current one and cancel out any changes caused by the previous one. It doesn’t change past history, so it’s the preferred approach without any risk of losing code

Git Revert can cancel a previous commit, so if you want to cancel more than one commit, you need to do the next-to-last COMMIT of git Revert

This is often used to revert when you commit a commit and then find something wrong with the commit

You can also use revert if you want to change a previous commit without affecting a later one. You can also use revert to put all of your previous commits in your workspace

So let’s simulate the environment

1. Cut to the Develop branch, which now has three COMMIT records

Try git Revert 16083CE with rever. If you’re using VS Code, you can see the workspace change and commit the default on the console

3. If you look at the log, you can see that a new entry has been added to Revert C, and the original C is still there

Commit Records are tagged

1, before launching, we need to tag the current COMMIT record so that the code can be rolled back in time if there is any problem

Let’s look at some common commands

1. Git tag Displays all the tag lists

Create a git tag with git tag [name]. Create a git tag with git tag

Git show [tag name]. For example, git show test 1

The end of the

Git management tools can be used every day in our daily life, so it is important to keep a firm grasp of common problem scenarios so as not to encounter a lack of money

Praise rich life, comment on three generations of beauty