10
头图

After rolling back the code on gitlab and pitting myself, I fell into thinking 🤔 "Who is the bug?"

Boy, have you clicked gitlab below 061d15b8b03739? This article will tell you how to use this button correctly.

image.png

1. Background

I originally planned to launch the function I developed at 3 p.m. on the xx, and merge the development branch into the master prepare to run the online pipeline, but unfortunately, the server classmates encountered some problems on this day, which caused the online time to be delayed. At the same time There are other branches on the front end that will go online today, and are ready to merge into the master branch. What I had to do at the time was to quickly roll back master

"mr" information I submitted contains the "revert" button. I know that this button is responsible for rolling back the code. I clicked this button if I was a ghost. Of course, the code was successfully rolled back and did not affect the follow-up classmates' online, but the story is not like this. Simple.

When the code plan I was responsible for went online again n days later, git merge branch on masetr , merge my branch actually failed, and the new part of my code could not be merged into master . At that time, the urgent brows were frowned into a "chuan". , Anxious Beast Evolution (🧬) Steel Anxious Beast.

image.png

Second, push the ashes and wipe out

At that time, I asked other students in the same group for help. My local branch was git merge master . At this time, my branch was up-to-date. Then I went to gitlab to disable master , forcibly pushed the branch content to master to cover, and then master The protection mechanism is reopened.

Of course there are problems with this set of operations. Is it okay to force master ? And this kind of operation needs to be reviewed by relevant personnel, which is "laborious + risky".

When all the dust settles, it’s time to start thinking, then why the code can’t be merged into master , and which step went wrong? At that time, my gitlab language was Chinese, and the button only showed "restore" two Word, I quickly switch the language to English, and the text of the button becomes "revert" at this time. I will "revert" to see how he is related to the absent "reset" .

Three, calm down and learn the difference between reset and revert

revert

Remove a commit , and generate a new record commit

Assume that the current branch state is as follows:

image.png

implement

git revert -n gt56th

git add .

git commit -m'feat: rm a'

image.png

reset

Move HEAD to a certain commit , commit after commit , and your local code remains unchanged.

Suppose the current branch state is as shown below
image.png

Execute git reset gt56th

image.png

Four, explain why the code does not fit in

By comparison, we can know why, that afternoon I could not merge the code into the master branch.

From the chart above steps, revert code is clearly documented in the deletion target commit is commit id to gt56th of this data, we then contain commit id to gt56th submitted merge to master a branch, git algorithm will determine the This commit has been removed, so git will think that my branch code is behind master , master removes this commit code is the latest.

git is no problem with this judgment. When we are developing with multiple people, a.js ' and'b' develop a project together,'a' deletes the 061d15b8b03a35 file and merge to master , the next day'b' changes other places Also merge to master , even if the code of b does not remove a.js , merge master branch will not add the a.js file.

Five, the default revert is strange

Let's talk about that since gitlab uses the revert function to roll back the code by default, that is to say, the official view is that this rollback method is the best, so where is it good?

First: continuity

Even if it is a rollback operation, it can be regarded master a normal operation reset will cause the missing of the timeline, so that we don't know what happened in the middle, which is not conducive to solving the problem in the long run.

Second: Someone pulls the code halfway

The code push arrived on master , and after 2 hours, a master code reset , everything seemed to be normal, but everyone didn’t know that the code of master pull was buried locally. 'b' performs master , and the code deleted master branch, causing the wrong code to go online.

Third: easy to roll back

For example, our gitlab default merge complete you delete the source branch, at this time we can directly pull master latest code, as can be git log find all the inside commit so afraid of playing the bad branch code found.

6. University questions in reset

After talking about a lot of the advantages and disadvantages of revert reset :

Suppose I have three files in my current project: a.js , b.js , c.js a.js is git commit -m'' , b.js is git add and c.js not monitored by git: 031d15b8b
image.png

image.png

git reset --hard CommitId (violent deletion)

This writing method directly HEAD back 061d15b8b03c3e to the target branch, and deletes all the code written after the current branch, which means that your code will be cleared.

This method is suitable for scenarios where certain codes are completely discarded, because you cannot find the deleted commit record git log

image.png

image.png

git reset --soft CommitId (comfortable?)

This way of writing directly HEAD back 061d15b8b03d11 to the target branch, and keeps your commit . These modifications are in the temporary storage area. We can continue to develop related functions and finally unify git add. && git commit -m '' once.

This method reminds me that we can usually submit multiple commit on our computer, but push we can go back first, and then combine commit and submit again.

image.png

image.png

git reset --mixed CommitId (default, this command is when we don't write parameters)

Restore the code after the target submission to an unmonitored state, that is, your code needs git add . . This move is equivalent to resetting the submission state, but there are some minor problems, such as some of us that have not been git add will not be separated from other files. It gives me the feeling that a lot of new code is inserted into our current code. If you use git status view it, it will all be red.

image.png

image.png

Seven, the process is sorted out to get a plan

Now that you understand the above knowledge points, you can derive a more reliable rollback process. When we want to temporarily roll back the codes from merge master gitlab revert button, and then change your local code git reset to the online commit version so that you can change these codes into the new commit , so you can apply for merge to master again.

8. The emergence and thinking of problems

When this problem first appeared, I used to think that gitlab problem with git and some algorithms of 061d15b8b03e0b. However, through systematic analysis, I realized that the problem was my own operation.
image.png

Setting the gitlab platform to Chinese makes it impossible to express some of the meanings that can be expressed in English. This is also a problem. It is better to write the code to know the original meaning of the code more clearly, rather than the translated meaning.

image.png

end

This is the case this time, I hope to make progress with you.


lulu_up
5.7k 声望6.9k 粉丝

自信自律, 终身学习, 创业者