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
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
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 repositorygit 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
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 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.
Revert 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To delete a Git repository, follow these steps: Confirm the repository you want to delete. Local deletion of repository: Use the rm -rf command to delete its folder. Remotely delete a warehouse: Navigate to the warehouse settings, find the "Delete Warehouse" option, and confirm the operation.

In order to securely connect to a remote Git server, an SSH key containing both public and private keys needs to be generated. The steps to generate an SSH key are as follows: Open the terminal and enter the command ssh-keygen -t rsa -b 4096. Select the key saving location. Enter a password phrase to protect the private key. Copy the public key to the remote server. Save the private key properly because it is the credentials for accessing the account.

Resolve: When Git download speed is slow, you can take the following steps: Check the network connection and try to switch the connection method. Optimize Git configuration: Increase the POST buffer size (git config --global http.postBuffer 524288000), and reduce the low-speed limit (git config --global http.lowSpeedLimit 1000). Use a Git proxy (such as git-proxy or git-lfs-proxy). Try using a different Git client (such as Sourcetree or Github Desktop). Check for fire protection

To download projects locally via Git, follow these steps: Install Git. Navigate to the project directory. cloning the remote repository using the following command: git clone https://github.com/username/repository-name.git

Connecting a Git server to the public network includes five steps: 1. Set up the public IP address; 2. Open the firewall port (22, 9418, 80/443); 3. Configure SSH access (generate key pairs, create users); 4. Configure HTTP/HTTPS access (install servers, configure permissions); 5. Test the connection (using SSH client or Git commands).

How to add a public key to a Git account? Step: Generate an SSH key pair. Copy the public key. Add a public key in GitLab or GitHub. Test the SSH connection.

Steps to update git code: Check out code: git clone https://github.com/username/repo.git Get the latest changes: git fetch merge changes: git merge origin/master push changes (optional): git push origin master

To fall back a Git commit, you can use the git reset --hard HEAD~N command, where N represents the number of commits to fallback. The detailed steps include: Determine the number of commits to be rolled back. Use the --hard option to force a fallback. Execute the command to fall back to the specified commit.
