Home > Article > Development Tools > Let you understand the git rollback code (detailed examples)
This article brings you relevant knowledge about GitRollback code. Git is an open source distributed version control system that can handle files from very small to very large efficiently and at high speed. Project version management, I hope it will be helpful to everyone.
In the daily coding process, merging between branches, rolling back, submitting, tagging and other operations are inevitable. If you still don’t know how to use the git tool to roll back Code, or always worried about making mistakes and not being sure about losing the code. This is very dangerous. After all, losing the code is a big deal. If it is small, it will deduct performance, but if it is serious, it will deduct many points. But don’t worry, you are lucky to read this article. , after you read it, you will no longer have code loss, because I will take you step by step to build branches to simulate common situations of rollback
Git (pronounced as /gɪt/ ) is an open source distributed version control system that can handle version management of projects from very small to very large efficiently and at high speed.
There are two commonly used methods in daily code rollback git revert
and git reset
to roll back. I will try my best to briefly and clearly introduce what these two commands can do for each of the different situations. Next, I will pull a new branch from the personal warehouse starting from 0 and build two branches, namely the main branch master
and development branch develop
to simulate
1, reset
is used when you want to submit commit
can be completely disappeared from the history record using
2. For example, if you submitted A-->B-->C## on the
master branch #Three records have been submitted. If there is a problem with record C at this time and you want to roll back to B, you can use
git reset
master branch and then perform functional development from
develop
master, if you find problems with the merged branches before going online, you can roll back the merged branches from
develop
develop This merger
master during collaborative development, you cannot use
reset to force a rollback
commit Because this will flush out other people's submission records, you can use
revert to perform the operation. We will talk about it below
Create a branch simulation environment
1. Create a new project from your own git repository and pull it locally2. Create an index.js and write something casually, and then submit it to the repository 3. We usegit log to view the commit in the terminal and we can see that there is currently only one commit
develop branch from the
master branch and switch to the branch
git checkout develop5. Add a piece of code to the
develop
6. Add a piece of code to the develop branch
7. Compare the latest commit records of the develop branch and the master branch. You can see that the dev branch is two ahead of the master branch. commit
Note that there is a problem here. When you merge branches, sometimes you will find that although the code is different, when merging branches, It prompts that the code has not been updated because the commit record of the current development branch lags behind the target branch to be merged. The reason for this is the abuse of reset, so reset must be used with caution
Operate reset to feel it
1. We merge the code of the develop
branch into master
and switch to the master
branch for execution git merge develop
2. We use git log
on the master branch to check the commit record and find the B record, and prepare to roll back this one. When rolling back, you don’t need to enter all the commits, usually the first 7 digits are enough
3. Here comes the key point. We use git reset 69fde2c
to roll back. At this time, we check the log record and find that the last is added c
. The record is gone. There is still a problem here. If you directly use git push
to push, the following prompt will appear.
#This is because the local record has fallen behind the warehouse code because of our rollback. This use requires
git push \-f
Perform forced submission
4. At this time, the master branch only has the commit records of A and B. This is a complete reset and rollback record. After that, we can continue to develop the develop branch normally. Merged into master
1. The principle of revert is to add a new submission after the current submission to offset the results caused by the previous submission. of all changes. It will not change the past history, so it is the preferred method without any risk of losing code
2. Revert can offset the previous submission, so if you want to offset multiple submissions, you need to execute git revert from the bottom A commit id, the penultimate commit
3, this is often used when you submit a commit and find that there may be a problem with the submission, you can use revert
4, There is also a situation where many people have already submitted code, but if you want to change a certain previous commit record without affecting subsequent commits, you can also use revert. It will put all the records you submit later into the workspace. It is only needed when merging. Pay attention to one thing
Let’s simulate the environment
1. Cut to the develop branch. Now the branch has three commit records
2. Let’s Try using rever to roll backgit revert 16083ce
. If you are also using vs code, you can see the changes in the workspace and submit the default commit
## in the console.
#3. Look at the log record, you can see that a new record has been addedRevert New C, and the original
New C is still there
1. Before going online, we need to tag the current commit record so that if there is any problem with the online code, it can be rolled back in time
Let’s introduce some commonly used commands
1.git tagList all tag lists
git tag [name], we add a new
git tag test 4, use
git tag Check it
git show [tag name], for example
git show test 1, if there is a problem after going online, we can roll back the code according to the commit id in the picture below
Git tutorial》
The above is the detailed content of Let you understand the git rollback code (detailed examples). For more information, please follow other related articles on the PHP Chinese website!