Our project uses GitLab for code management, and when I upgraded the system to SpringBoot 2.0, the branch was merged to the Master branch. During the actual deployment, I found that a child dependency of a two-party package that I depended on had not been upgraded, causing a service to fail to drop through. Since it took time to fix the bipartite package, we decided to Revert the Master branch in order not to affect the subsequent release of other features.

The next day, after fixing the bipartite package issue, I resubmitted the Merge request, only to find that the only changes committed were to the bipartite package, and no other code changes were made.

Problem recurrence

The following mockup replicates the problem. I created a brand new project. The first commit was made in the Master branch and the commit read

1
First Commit From Master

Then a new branch is created dev and the following is appended to the commit:

1
Second Commit From Dev

merge the dev branch to the master branch, whereupon the master branch will look like this.

1
2
3
First Commit From Master

Second Commit From Dev

At this point, I think the dev branch is not finished yet, so I want to revert it and recommit it. So find the Merge request in GitLab and click the Revert button, as shown below.

Clicking Revert is equivalent to Revert the code to a new branch and Merge that new branch to the Master. The Commit record is shown below, where revert-f8856e36 is the revert branch.

Switch to the Master branch after Revert and find that the code has indeed been rolled back.

At this point, switch to the dev branch and fill in another line at the end, which reads

1
Third Commit From Dev

Then re-initiate the Merge to Master request for the dev branch.

But I found that the difference between dev branch and master branch commit is only the commit record after dev branch revert.

When I go to the Changes page, I see that the changes are only recorded after revert. The line Second Commit From Dev before revert is not considered a change and is considered to exist on the Master branch?

The above completes the reproduction of the problem, the Second Commit From Dev line is lost, to be precise, the dev branch revert before the content is lost.

Solutions

The solution was obtained by referring to this article.

The core idea is to revert the commit record of revert again, so let’s start experimenting.

First switch to the Master branch and pull out a branch revert_tmp based on the Master branch, this branch is used to save the commit records of revert revert.

1
~/Projects/demo: git checkout -b revert_tmp

Find that commit record of revert, note that there will be two records related to revert, the first one is revert and the second one is the record of merge after revert, here take the first one, as shown in the figure below.

1
2
3
~/Projects/demo: git revert f5c3b544164eec662ea6914d6bd19aedf46874f8
[revert_tmp aea128e] Revert "Revert "Merge branch 'dev' into 'master'""
 1 file changed, 3 insertions(+)

Then switch back to the dev branch and merge the branch revert_tmp to the dev branch.

1
2
3
~/Projects/demo: git checkout dev
~/Projects/demo: git merge revert_tmp
~/Projects/demo: git push

After pushing the dev branch to the remote, we recommit the merge request to the master. We find that the commit record before revert has not been retrieved, i.e. the second commit by dev record has not been retrieved, as shown below.

This is due to the feature of revert, although this commit record is not recovered, the code before revert is back, as shown below.


Reference https://jitwxs.cn/38727be2.html