Das Verstaendnis des Unterschieds zwischen git rebase und git merge ist fuer jeden Entwickler wichtig.
Wie Git Merge funktioniert
Git merge erstellt einen neuen Merge-Commit mit zwei Eltern.
Beim Ausfuehren von git merge feature findet Git den gemeinsamen Vorfahren.
Wie Git Rebase funktioniert
Git rebase schreibt die Historie um.
Git identifiziert den gemeinsamen Vorfahren und wendet Commits einzeln an.
Visueller Vergleich
BEFORE (both branches have diverged from common ancestor C2):
C5---C6---C7 (feature)
/
C1---C2---C3---C4 (main)
AFTER git merge feature (from main):
C5---C6---C7
/ \
C1---C2---C3---C4----M8 (main) â merge commit M8 has 2 parents
(feature still points to C7)
AFTER git rebase main (from feature):
C1---C2---C3---C4 (main)
\
C5'---C6'---C7' (feature) â new commits (different SHAs)
Then fast-forward merge:
C1---C2---C3---C4---C5'---C6'---C7' (main, feature) â linear historyFunktionsvergleich
| Aspekt | git merge | git rebase |
|---|---|---|
| Historie | Gesamte Historie erhalten | Lineare Historie |
| Commit-SHAs | Original-SHAs erhalten | Neue SHAs erstellt |
| Sicherheit | Nicht-destruktiv | Destruktiv |
| Konfliktloesung | Einmal loesen | Pro Commit loesen |
| Team-freundlich | Sicher fuer geteilte Branches | Gefaehrlich bei geteilten Branches |
| Rueckgaengig | Einfach | Schwieriger |
| git bisect | Merge-Commits stoerend | Saubere Historie hilfreich |
| Graph | Komplexer Graph | Saubere Linie |
Git Merge im Detail
Fast-Forward Merge
Wenn der Zielbranch keine neuen Commits hat.
# Fast-forward merge (no merge commit created)
git checkout main
git merge feature
# Before:
# C1---C2 (main)
# \
# C3---C4 (feature)
# After:
# C1---C2---C3---C4 (main, feature)
# main pointer simply moved forwardDrei-Wege-Merge
Wenn beide Branches divergiert sind.
# Three-way merge (creates merge commit)
git checkout main
git merge feature
# Before:
# C3---C4 (feature)
# /
# C1---C2---C5---C6 (main)
# After:
# C3---C4
# / \
# C1---C2---C5---C6---M7 (main) â merge commit M7--no-ff
Erzwingt einen Merge-Commit.
# Force merge commit even when fast-forward is possible
git checkout main
git merge --no-ff feature
# Before:
# C1---C2 (main)
# \
# C3---C4 (feature)
# After (with --no-ff):
# C1---C2---------M5 (main) â merge commit preserves branch history
# \ /
# C3---C4 (feature)
# After (without --no-ff, default):
# C1---C2---C3---C4 (main, feature) â no evidence of branchGit Rebase im Detail
Einfacher Rebase
Wendet Commits auf den Zielbranch an.
# Basic rebase workflow
git checkout feature
git rebase main
# Before:
# C3---C4 (feature)
# /
# C1---C2---C5---C6 (main)
# After rebase:
# C3'---C4' (feature) â new commits!
# /
# C1---C2---C5---C6 (main)
# Then merge (fast-forward):
git checkout main
git merge feature
# C1---C2---C5---C6---C3'---C4' (main, feature) â linear!Interaktiver Rebase
Commits aendern, zusammenfassen, umordnen.
# Interactive rebase - clean up last 4 commits
git rebase -i HEAD~4
# Editor opens with:
pick abc1234 Add user model
pick def5678 Fix typo in user model
pick ghi9012 Add user validation
pick jkl3456 Fix validation edge case
# Change to:
pick abc1234 Add user model
fixup def5678 Fix typo in user model # squash into previous, discard message
pick ghi9012 Add user validation
fixup jkl3456 Fix validation edge case # squash into previous, discard message
# Result: 2 clean commits instead of 4
# "Add user model" (includes typo fix)
# "Add user validation" (includes edge case fix)
# Interactive rebase commands:
# pick = use commit as-is
# reword = use commit but edit message
# edit = use commit but stop for amending
# squash = meld into previous commit (keep message)
# fixup = meld into previous commit (discard message)
# drop = remove commit entirely--onto Rebase
Subset von Commits auf neue Basis.
# --onto: Move a branch to a different base
# Scenario: feature-b was branched from feature-a by mistake
# You want feature-b based on main instead
# D---E (feature-b)
# /
# A---B---C (feature-a)
# |
# F---G (main)
git rebase --onto main feature-a feature-b
# Result:
# D'---E' (feature-b) â now based on main
# /
# A---B---C (feature-a)
# |
# F---G (main)Konfliktloesung
Beide koennen Konflikte erzeugen.
Merge-Konflikte
Alle Konflikte auf einmal.
# Merge conflict workflow
git checkout main
git merge feature
# CONFLICT (content): Merge conflict in src/app.ts
# Automatic merge failed; fix conflicts and then commit
# 1. Open conflicting files and resolve
# 2. Stage resolved files
git add src/app.ts
# 3. Complete the merge
git commit # creates merge commit with conflict resolution
# Abort merge if needed
git merge --abortRebase-Konflikte
Konflikte pro Commit.
# Rebase conflict workflow
git checkout feature
git rebase main
# CONFLICT in commit C3: Merge conflict in src/app.ts
# 1. Resolve conflict in src/app.ts
git add src/app.ts
# 2. Continue rebase to next commit
git rebase --continue
# May hit another conflict in C4...
# CONFLICT in commit C4: Merge conflict in src/utils.ts
git add src/utils.ts
git rebase --continue
# Abort rebase if it gets too complex
git rebase --abort # returns to pre-rebase state
# Skip a problematic commit during rebase
git rebase --skipTeam-Workflows
Merge-basierter Workflow
Der gaengigste Team-Workflow.
# GitHub Flow (merge-based)
git checkout -b feature/add-auth
# ... make commits ...
git push -u origin feature/add-auth
# Open PR on GitHub
# Review + approve
# Click "Merge pull request" (creates merge commit)
# Or "Squash and merge" (single commit)Rebase-dann-Merge Workflow
Rebase zum Aktualisieren, dann Merge.
# Rebase-before-merge workflow
git checkout feature/add-auth
# ... make commits ...
# Before opening PR, update with latest main
git fetch origin
git rebase origin/main
# Force push (safe because it's your own branch)
git push --force-with-lease origin feature/add-auth
# Open PR on GitHub
# Merge with --no-ff to record integration point
git checkout main
git merge --no-ff feature/add-authSquash and Merge Workflow
Alle Commits zu einem zusammenfassen.
# Squash and merge (via GitHub UI or CLI)
git checkout main
git merge --squash feature/add-auth
git commit -m "feat: add authentication system"
# Before:
# main: A---B---C
# feature: A---B---D---E---F---G
# After squash-merge:
# main: A---B---C---H â H contains all changes from D+E+F+G
# (feature branch can be deleted)Die goldene Regel des Rebase
Nie bereits gepushte Commits auf geteilten Branches rebasen.
# DANGEROUS: Rebasing a shared branch
git checkout shared-feature
git rebase main
git push --force # !! This rewrites history for everyone!
# SAFE: Rebasing your own local branch
git checkout my-local-feature
git rebase main
# No push yet, or push --force-with-lease to your own branch
# SAFE: Using --force-with-lease instead of --force
git push --force-with-lease origin my-feature
# Fails if remote has commits you haven't seenBest Practices
Git Rebase vs Merge Best Practices:
1. Use merge for integrating feature branches into main
- Creates clear integration points
- Safe for shared branches
- Easy to revert entire features
2. Use rebase to keep feature branches up-to-date
- git rebase main (before opening PR)
- Creates clean, linear history
- Makes code review easier
3. Use interactive rebase to clean up before PR
- Squash fixup commits
- Reword unclear commit messages
- Drop debugging commits
4. Use git pull --rebase as default
- Avoids unnecessary merge commits
- git config --global pull.rebase true
5. Never rebase shared/pushed commits
- Only rebase your own unpushed work
- Use --force-with-lease, never --force
6. Use squash-merge for feature branches
- One clean commit per feature on main
- Detailed commits preserved in PR history
7. Use --no-ff for important merges
- Preserves the fact that a branch existed
- Makes git log --first-parent usefulHaeufig gestellte Fragen
Wann rebase vs merge?
Rebase fuer lokale Updates, Merge fuer Integration.
Ist Rebase gefaehrlich?
Auf geteilten Branches ja, lokal nein.
Was ist Squash and Merge?
Alle Commits zu einem zusammenfassen.
Kann man Rebase rueckgaengig machen?
Ja, mit git reflog.
Rebase oder Merge fuer Teams?
Hybrider Ansatz empfohlen.
Was ist git pull --rebase?
Rebase statt Merge beim Pull.
Verwandte Tools und Anleitungen
- JSON Formatter - Format Git config files
- Diff Checker - Compare code changes before merge
- Git Commands Cheat Sheet
- Git Branch Naming Convention
- Git Cherry-Pick, Revert, and Reset Guide
- Git Workflow Strategies