SoFunction
Updated on 2025-04-14

How to solve the problem of merge code loss after git revert

Git revert and merge code loss problem

Problem scenario

The company uses gitlab as a code management tool. When the developer's new function development ends (development branch: feature-member), the merged code needs to be submitted mr(pr) to the relevant person in charge, and the person in charge merges the code into the master.

At this time, the product said that this version will not be launched for the time being and will be released together with the next version. In order to ensure the accuracy of the master branch code, we use git revert to cancel this mr.

Then after the next version is developed, I submitted mr and found that the changed files are only changed after revert, and the first file changes of mr are gone (come again after reverting, git will discard this code by default).

Solution

# Switch to master branchgit checkout master
git pull
# Pull out a branch based on master revert_tmpgit checkout -b revert_tmp

# Revert the commit before git revert (commit number can be found from git log)git revert acd414e1cd42315ce93a9730db961155be140013
git checkout feature-member
git merge revert_tmp
git commit -m "revert"
git push

After the above operation, mention pr again so that the code before revert can be seen for the first time.

git revert one time merge and then merge code is washed out

question

There is a problem with the code when you find that a feature branch of merge is found in the develop branch (if there is a problem with the code of the feature branch, just modify the code of the feature branch and then merge to develop.

If it is merge, the conflict is not resolved, you need to revert this merge), so you use the 'git revert < commit id > -m 1' command to revert this merge, but when you want to redirect this feature branch to the develop branch, you find that you can't merge.

Solution

After revert feature merge, checkout to feature branch,

Use git reset head^ to revoke the last commit (revoke all commits if there are multiple commits before merge)

Then modify the problematic code. If the code is fine, it's OK to not modify it if it's fine.

After modification, submit the code and push it to the remote branch

git add .
git commit -m “”
git push -f

Checkout develop and switch to develop branch

Use git merge to merge the feature branch again, and you will find that the code has been merged

Finally, push to the remote will be OK.

Summarize

These are just personal experience. I hope you can give you a reference and I hope you can support me more.