Problem scenario

In practice, when you go online, you submit the merge to Master request on GitLab for your new function code. After the leader with merge permission approves, your code will merge with master. Okay, QA starts to put your code online, and bam, bam, the alarm goes off, there’s something wrong with the code. The lead revert your code to make sure the master branch is correct. When your branch pulls the master, you find that the new function code on your branch is gone!! How to find

This paper tries to solve this problem from the Angle of actual combat

conditions

  1. Suppose you already have oneGit project: test-gitAnd has two branches:The master, the test

In actual combat

In the test branch, you create a file welcome with the text :hi git. Git commit -m ‘test Git Revert’ commit

Currently, the contents of the master and Test branches are as follows

~/tt/test-git>>master $ ll
drwxr-xr-x  3 tt  staff    96B  5  8 20:54 test
-rw-r--r--  1 tt  staff    16B  8  1 19:55 test3.log
Copy the code
~/tt/test-git>>test $ ll
drwxr-xr-x  3 tt  staff    96B  5  8 20:54 test
-rw-r--r--  1 tt  staff    16B  8  1 19:55 test3.log
-rw-r--r--  1 tt  staff     7B  8  1 20:19 welcome

~/tt/test-git>>test $ cat welcome
hi git
Copy the code

Now, tomastersubmitmerge to masterRequest, as shown below

leadershipmergeAnd then you start going online, assuming there’s a problem on the line, leaderrevertThe submitted code. The following figure

Then you need to look at the problem, so you’ll do it locally, go back to your code branch and execute git merge Origin /master

~/tt/test-git>>test $ git merge origin/master Updating 670adc2.. d3961f7 Fast-forward welcome | 1 - 1 file changed, 1 deletion(-) delete mode 100644 welcome ~/tt/test-git>>test $ ll total 8 drwxr-xr-x 3 tt staff 96B 5 8 20:54 test -rw-r--r-- 1 tt staff 16B 8 1 19:55 test3.logCopy the code

At this point, you realize that the code you submitted is gone. Welcome file is missing. What should I do

How do I retrieve the code? So let’s start retrieving

  1. git logFind the leadershiprevertThe one you codedcommit idHere,commit idis50a06845da879ab76e6fdd55dce923826742dcb2. Figure 3 belowcommitinstructions

  1. git revert --no-commit 50a06845da879ab76e6fdd55dce923826742dcb2

Your code is already back, and then it’s gone. Easy

conclusion

Find the commit ID for the lead to revert your code and git Revert –no-commit commit ID

To sum up: revert the commit