Ela
Ela

Reputation: 419

Delete few commits in Git branch before merge into master

We have three main branches. develop, uat and master. We are in the last week of current sprint. It has been decided to postpone one feature to the next release. That feature is already in uat environment. So team wants to keep it in uat branch. So before merging into master, I have to remove that feature which has 3 commits.

I can create a release branch from uat, revert those 3 commits and merge into master. But next time conflict might occur in master branch since usual merge UAT into master. In addition to that, release branch is a temporary branch that will be created for this scenario and it will be deleted after merge.

I don't want to mess up with master branch. Please suggest if there is any better way to handle this scenario.

Upvotes: 0

Views: 287

Answers (1)

Marina Liu
Marina Liu

Reputation: 38136

The workflow you are using seems: continuous build/deploying for uat branch, and then merge uat branch into master for a new release.

So I will suggest backup current uat branch, remove the three commits on uat branch. After merging uat branch into master, recovery/reapply the three commits on uat branch again for next release.

There have two situations for you to change and recovery/reapply uat branch. You can follow one the below situations you meet.

Situation 1: the three commits are the latest three commits on uat branch

Assume the commit history looks as below, and the commit D, E and F are the commits for the feature that you decide to postpone for next release.

…---A---B            master
…---C---D---E---F   uat

You can use below commands to backup current uat branch and then remove the three commits on uat branch:

git checkout -b temp uat
git checkout uat
git reset --hard HEAD~3

Then the commit history looks like:

…---A---B         master
…---C             uat
     \
      D---E---F   temp

And now, you can merge uat into master for the new release, so the commit history will be:

…---A---B---G           master
           /
…---------C             uat
           \
            D---E---F   temp

Then you can recovery uat branch as original by

git checkout uat
git merge temp
git branch -D temp

Then the commit history looks as below, and the three commits D, E and F will apply for next release when merge uat into master for next time.

…---A---B---G             master
           /
…---------C---D---E---F   uat

Situation 2: the three commits are not the latest commits, there has other commit(s) after the three commits

Assume the commit history as below, and the commits D, E and F are the commits you want to remove. And there have commits G and H after the three commits.

…---A---B    master
…---C---D---E---F---G---H   uat

To backup current uat and remove the three commits on uat branch, you can use below commands:

git checkout -b temp uat
git checkout uat
git rebase --onto uat~5 uat~2 uat

The commit history will be:

…---A---B         master
…---C---G'---H'   uat
         \
          D---E---F---G---H   temp

After merge uat into master, the commit history looks like:

…---A---B------I   master
              /
…---C---G'---H'   uat
     \
      D---E---F---G---H   temp

Then you can re-apply the three commits D, E and F on uat branch by below commands:

git checkout temp
git reset --hard HEAD~2
git rebase uat temp
git checkout uat
git merge temp
git branch -D temp

Then the commit history will be:

…---A---B------I    master
              /
…---C---G'---H'---D'---E'---F'   uat

And the three commits D', E' and F' can be release for next time.

Upvotes: 1

Related Questions