Git How to apply changes made to the root branch that has already merged

eye-catch Other techs
Sponsored links

Branching strategy for a big feature

When working on a big feature, it should be separated into small tasks so that we can easily understand and implement each small task. Let’s look at an example.

parent-task has the following subtasks.

  • subtask 1
  • subtask 2
  • subtask 3
  • subtask 4

The end of the each task, we will create a PR and merge those changes onto the main branch after squashing the commits. So the commit history will look like the following.

This strategy works fine if the changes doesn’t cause any side effect to the existing code that could cause a bug. Additionally, each change might not make sense until all tasks are done. So my team follows the following strategy.

  1. Create a branch-parent from main branch
  2. Create a branch branch-subtask-1 from branch-parent
  3. Merge branch-subtask-1 onto branch-parent
  4. Repeat 2 and 3 until all subtasks are done
  5. Rebase and Merge branch-parent onto main branch

This is the basic strategy. We can squash the commits when merging the branch-parent when it’s better unit size of the commit.

Sponsored links

Problem when creating a branch from a branch

When each code review is fast enough, we can create a new branch from the root branch branch-parent. However, it takes some days in some cases for whatever reasons.

We need the previous changes from subtask 1 to make additional changes for subsequent subtasks. In this case, the branch needs to be created from the previous branch branch-subtask-1. So, a branch has a child branch that has a child branch. Additional commit could be added to the branch-subtask-1 while working on branch-subtask-2. So the Git history will look like this.

I basically rebase my branch when a PR is merged. If branch-subtask-1 is merged onto branch-parent, branch-subtask-2 needs to be rebased onto branch-parent to apply all changes. However, git shows conflict because branch-subtask-2 contains all commits from branch-subtask-1. If the same code was modified in more than two commits, rebasing causes conflict because the final code is different from commit 1-1. It takes a while to resolve it if each commit causes conflict.

I want to avoid the many conflicts for each commit during the rebase. How should we resolve this?

Solutions

There are two choices.

Rebase and squash commits

This can be done only before the parent branch is merged.

If branch-subtask-1 is not merged yet

  1. Rebase branch-subtask-2 onto branch-subtask-1 to apply all changes
  2. Squash all commits from branch-subtask-1
  3. Rebase branch-subtask-2 onto branch-parent

Since the squashed commit is identical to the commit subtask 1 on branch-parent, it doesn’t cause conflict.

Merge the change to the branch

If the parent branch has already been merged, merging it to branch-subtask-2 is easy.

In this way, we need to resolve conflict only once. It’s easy to compare the code.

Comments

Copied title and URL