Home  >  Article  >  Development Tools  >  Let you understand the git rollback code (detailed examples)

Let you understand the git rollback code (detailed examples)

WBOY
WBOYforward
2022-02-15 17:39:296755browse

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.

Let you understand the git rollback code (detailed examples)

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

Introduction

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.

Preface

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

reset introduction

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

3. This command is most likely to be used. Our main branch, because our online branch is generally the

master branch and then perform functional development from develop

4. After the development is completed, merge the branch into

master, if you find problems with the merged branches before going online, you can roll back the merged branches from develop

5. To put it bluntly, cancel

develop This merger

6. However, there is a situation where when everyone is merged into

master during collaborative development, you cannot use reset to force a rollbackcommit 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 locally

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

Let you understand the git rollback code (detailed examples)

3. We use

git log to view the commit in the terminal and we can see that there is currently only one commit

Let you understand the git rollback code (detailed examples)

4. We move out a

develop branch from the master branch and switch to the branch git checkout develop5. Add a piece of code to the develop

branch. At this time, a new record of B is added to the commit record of develop

6. Add a piece of code to the develop branch Let you understand the git rollback code (detailed examples)

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. commitLet you understand the git rollback code (detailed examples)

Let you understand the git rollback code (detailed examples)Let you understand the git rollback code (detailed examples) 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

Let you understand the git rollback code (detailed examples)

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.

Let you understand the git rollback code (detailed examples)

#This is because the local record has fallen behind the warehouse code because of our rollback. This use requires git push \-fPerform 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

Let you understand the git rollback code (detailed examples)

Introduction to revert

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 recordsLet you understand the git rollback code (detailed examples)

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. Let you understand the git rollback code (detailed examples)

#3. Look at the log record, you can see that a new record has been added

Revert New C, and the original New C is still there

Let you understand the git rollback code (detailed examples)

Tagging commit records

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

Let you understand the git rollback code (detailed examples)

2. Create a tag, use

git tag [name], we add a new git tag test 4, use git tag Check it

Let you understand the git rollback code (detailed examples)

3. Check the commit information corresponding to the tag,

git show [tag name], for examplegit 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

Let you understand the git rollback code (detailed examples)

End

git management tool It can be said that we use it every day, so we must firmly master the methods corresponding to common problem scenarios to avoid being overwhelmed when encountering

Recommended study: "

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete