Git Thoughts - Part I - Merge Branch A to Branch B or Rebase B to top of A


I've started a branch, the name is feat A and it was “born” from the main branch. I've started working on my feat A.


Meanwhile Jarvas started feat B, also “born” from the main branch.


Jarvas finished his changes and created a pull request that was approved. His branch was “closed”: reintegrated on the main branch and the old branch reference deleted.

 

Option One - Rebase feat A to the top of Main Branch

  • Re-apply commits from feat A on the top of Main Branch.

This process is done, commit to commit. Following the order of commits in feat A, each commit is applied to the top of the Main Branch. If it detects any conflict that cannot be resolved automatically, the process is paused and it's necessary the user intervention to solve it and continue the Rebase with the next commit.

Gif with visual example of rebasing a branch to top of another
Fig. 2  Rebase feature branch

Option two - Merge from the main branch to feat A

  • Creates new commit on feat A with the new commit refs from Main Branch.

This process is done in one step. If Git detects any conflict that cannot be resolved automatically, it’s necessary the user intervention to solve it and finish the merge.

 

Gif with visual example of merging to update feature branch
Fig. 1 - Merge to update feature branch


I prefer Option one - Rebase. In my opinion it will result in a more clean repository tree and more easy to understand "who comes first" when dealing with feature dependency and repository history.

 

Note:

I've used https://learngitbranching.js.org/ for the Git tree example in the Gif. Both gigures don't represent the example described and the start of the article. The intent was only to give a visual example for the differences between using Rebase or Merge in this use case.

Git Documentation links:

https://git-scm.com/docs/git-rebase
https://git-scm.com/docs/git-merge

Comentários

Mensagens populares deste blogue

Semantic HTML for a conscient Development.

Git Thoughts - Part II - What is rebase?

Testing and automated tests.