Collaborative coding, you code and submit your code to your “branch”, but there must be a time when you need to integrate your code with other programmers’ code. Git is one tool that can make life easier, by providing seamless ways to join codes.
Wait… there are more than one ways to do that? Yeah, for joining codes git has command merge and rebase. In git experiences, programmers must have been used merge before, but what rebase really is?
Integrating the codes
Both of merge and rebase do same thing, integrate changes to another branch, but they do it in very different manner. That’s why we have two options here: merging or rebasing.
master & feature branch in need to integrate
Merging: like fossil fuel, easy but poluting the environment
The easiest way to integrate changes is to merge. Merge will squash changes to the destination branch and produce “merge commit”. The command will not change existing branches structure, that’s why merge is usually called “non-destructive operation”.
merge: will not change project structure but will have commit messages
It seems very easy with merge, but merge will make the branch (especially master where changes are usually merged into) become dirty with tons of merge commits. It can make hard for programmers to track history of the project.
Rebasing: Clean Energy of the Future
Rebasing, in the nutshell, is moving most recent commits to the top of previous commit. This command will produce straight line branch history. It will make developers easier to track changes with other commands, for example: git log.
rebase: integrate commits into single linear line
One trade-off of rebasing is it may re-write the project history, making developers even more confuse. Rebasing should be fine if all programmers do it carefully and follow the golden rule of rebasing.
Golden Rule of Rebasing
The golden rule of rebasing is simple: never do rebase if there are two or more programmers working in same branch. When someone rebase the branch, the structure of the branch will be changed, but other people history is still the original structure. It will result in “diverged”. This thing can only be solved using merge, resulting more and more merge commits.
Do rebase only if nobody except you working on the branch.
— Golden Rule of Rebasing
Remember, when doing rebase, do it with caution. It will make the project history cleaner and easier to understand.