Git Merge vs. Git Rebase
When working with Git, you often create a branch to work on a feature. Eventually, you must combine that work back into the main branch. You have two choices: Merge or Rebase.
Git Merge
git merge feature
- What it does: It takes the content of the feature branch and the main branch and ties them together with a new "Merge Commit."
- History: Non-destructive. It preserves the exact history of when commits happened.
- Visual: It looks like a diamond shape in your history graph.
A---B---C feature
/ \
D---E---F-------G main
Pros: True history. You see exactly when the branch started and finished.
Cons: Cluttered history. If you have 50 developers, the history becomes a mess of crossing lines ("Guitar Hero" graph).
Git Rebase
git rebase main
What it does: It moves the entire feature branch to begin at the tip of the main branch. It rewrites history.
History: Linear. It looks like you wrote your code after the latest changes in main.
code
Text
D---E---F---A---B---C main (feature is gone, commits moved)
Pros: Very clean history. Easy to read.
Cons: Dangerous. You are rewriting history. If you rebase a branch that others are sharing, you will break their code.
The Golden Rule
Never rebase a public branch. Only rebase your own local work before pushing it.