Git: Merge and Rebase
Merge and rebase are two ways to integrate changes from one branch into another.
Merging
Merge a branch
git switch mainSwitches to the main branch.
git merge feature-branchIntegrates commits from the 'feature-branch' into the current branch using a merge commit. This preserves the history of both branches.
Rebasing
Rebase a branch
git switch feature-branchSwitches to the 'feature-branch'.
git rebase mainMoves the entire 'feature-branch' to the tip of 'main', creating a linear history. This rewrites the commit history of the 'feature-branch'.
Difference Between Merge and Rebase
Merge:
- Combines the histories of two branches.
- Creates a merge commit if there are changes on both branches.
- Preserves the complete history of both branches.
- Reapplies commits from one branch onto another.
- Rewrites commit history to create a linear sequence.
- Useful for cleaning up commit history before merging into a main branch.
- Can be more complex if there are conflicts, as it requires resolving them for each commit.