[Git] Code mistakenly pushed and restored (real project environment, not just talking on paper)

background

RT, my eyes were blurred today, and I accidentally [merged] the working branch into an irrelevant feature branch, and the code has been pushed to the remote warehouse. So, you can only try to revert to the last commit.
[Merge] There is a point in the branch that is inevitable for us, and it is difficult to describe in words. Let me draw a picture for everyone to see, as follows:

insert image description here
Everyone should know that there will be this phenomenon, and you should be able to understand it clearly by looking at the picture.
Moreover, since multiple colleagues have their own branches during the development process, there will be extremely intricate branch lines after the merger, which is a headache to watch.

Solutions

But the problem has to be solved. First of all, do you know what commands you need to use if you want to restore?
I believe everyone has Baidu, usually you will get the following two commands:

  1. Reset the current project code to a certain version:git reset --hard [版本id]
  2. Force a commit to the previous version:git push origin [branch] --force

Inexperienced friends may be a little confused, what is the version id, and how to determine it?

what is the version id

I believe most people can understand this question. That is, every time we submit the code to the warehouse, in order to distinguish the difference between this submission record and the previous one, we must have a version number mark, which is the version number. If you are an idea user, you can look at the picture below to get the [incomplete version number] of the current submission: as shown in the
insert image description here
figure below, it means that the [incomplete version number] of the [modified date format parameter] submitted by my colleague this time It is: 9c948858(Although it is an incomplete version number, it is already available. The probability of conflicting version number prefixes is not very high.)
Of course, we can also obtain the complete version number, which is simpler. Align directly to the current submission record, right click and select-"Copy Revision Number, you can also get
insert image description here

how to determine

How to determine that this is the version you want to roll back is up to you. But as I said earlier, since multi-branch development is bound to result in intricate commit records, the most difficult part is sometimes the process of finding this rollback point.
Here I will talk about how I found it, it is very simple and very tricky. After I found out that I made a push error, I asked my colleague not to update the code first, and asked him to send me the version number in his local warehouse first, hahaha.

implement

Now that I have the version number, I can start executing commands.
As follows
: Execute first: git reset --hard 8a837758e2316f260957d658326e82e16c14862c
Then execute: git push origin yzmyd_branch_0824 --force
The latter --forceis the meaning of forced push, without this, you will be prompted for version conflicts

Guess you like

Origin blog.csdn.net/qq_32681589/article/details/132470375