1. git merge – Combine histories together
🔹 What It Does:
• Merges another branch into your current branch.
• Keeps the history of both branches.
• May create a merge commit if branches diverged.
________________________________________
✅ Example:
Assume:
• You are on master
• You have a feature branch feature-1
git checkout master
git merge feature-1
📘 Output (if no conflict):
Merge made by the 'recursive' strategy.
file1.js | 4 ++++
1 file changed, 4 insertions(+)
If changes conflict, Git will prompt you to resolve them.
________________________________________
🌳 Resulting History (MERGE)
c3d9a61 (HEAD - master) Merge branch 'feature-1'
|\
| * a1b2c3d (feature-1) Feature commit
| abc1234 Previous master commit
________________________________________
🔁 2. git rebase – Reapply commits on top of another base
🔹 What It Does:
• Moves your branch to a new base (usually the latest main/master).
• Rewrites commit history.
• Results in a linear history (no merge commit).
________________________________________
✅ Example:
Assume:
• You are on feature-1
• You want latest master changes before pushing
git checkout feature-1
git rebase master
📘 Output (if clean):
First, rewinding head to replay your work on top of it...
Applying: Feature commit
________________________________________
🌳 Resulting History (REBASE)
a1b2c3d (feature-1) Feature commit (rebased)
abc1234 (master) Previous commit
Now your commit appears as if it was made after the master branch.
________________________________________
🔄 Comparison Table
Feature git merge git rebase
History Keeps all commits (with merge commits) Linear, clean history
Use Case Safe for public branches Great for local cleanup
Workflow Combine branches Reapply on top of latest
Merge commit Yes (if branches diverged) No
Conflict resolution May need to resolve once May need to resolve each commit
________________________________________