Git Merge v/s Git Rebase

Git Merge v/s Git Rebase

Git merge

'Merge', the word spells out that one has to combine two different things. Like rivers merge into one big river.

In git merge is used to merge a temporary branch to the main/master branch.

Git Merge is used to combine changes from one branch into another. The main purpose of merging is to bring together the work done in separate branches to the main or required branch.

Now Imagine you have a project with a main branch and a feature branch. Once you complete the feature development, you initiate a merge to integrate the changes into the main branch. Git will analyze the differences between the branches and automatically apply the changes to the main branch.

Git Rebase

Imagine you work in a company. There are 10 developers in the company and you are one of them. I go for a 1-month vacation and when you return you find that the code base of your company has moved too forward, so what would you now do to come in phase with the other developers? You can use git rebase. With this command, you can sync your work and your main branch work in a sequence of commits to understand how the work proceeded when you were absent.

Still confused?

Let's understand with one more example.

Credits: https://www.simplilearn.com/what-is-git-rebase-command-article

Let's say you're a developer who is working on a new feature on a dedicated branch. Then, another development team member updates the main branch with some new commits. The situation looks like this:

Git_Rebase_1.

Eventually, however, the team concludes that the main's new commits are relevant to the feature you are working on. So you can merge your commit with the main branch.

Git_Rebase_2.

But you decide to rebase it and if you use Git rebase, you move your whole feature branch, starting it on the tip of the main branch so that all the new commits are now part of the whole. This rewrites a whole new history. It makes the feature branch now the main branch. Now there is no more feature branch.

Git_Rebase_3.

This can be dangerous too because if there are other branches and since the history is written, there can be confusion created among developers.

Finally, Git merge v/s Git rebase

Git MergeGit Rebase
Combines the changes from two branches, creating a new "merge commit" to integrate them.Moves the current branch's commits on top of another branch, rewriting the project history as if the commits were made sequentially.
Preserves the original commit history of both branches, resulting in a branching structure.Produces a linear commit history without additional merge commits, providing a cleaner and more straightforward history.
Safer for collaboration as it preserves the individual branches' integrity.Can cause issues with collaboration, as it rewrites history and can lead to conflicts.
Creates a branching network that shows the relationship between branches.Produces a cleaner, more linear network with fewer branch divergence points.
Keeps original commit IDs intact.Creates new commit IDs for each commit, effectively changing commit history.
Requires resolving conflicts in the merge commit, which can be moreConflicts arise for each original commit, potentially leading to more conflicts to resolve.
Reverting a merge requires creating a new "revert commit."Can easily undo changes by resetting the branch to the previous state.
Recommended for long-lived branches or collaboration with multiple developers.Recommended for short-lived feature branches or when maintaining a clean commit history.
Offers more flexibility in the workflow, allowing for a more relaxed development process.Enforces a disciplined and linear development workflow, keeping the history cleaner.
Relatively easy to understand and use, suitable for beginners.Requires a better understanding of Git and its implications, more suitable for experienced users.
Easily incorporates changes from the main branch into the feature branch.Allows for smoother integration of upstream changes as the feature branch's commits are reapplied.

At last from my learnings, I would suggest using git merge is safer and more useful than using git rebase.