~/
hackweb.dev
Merging Branches
Quiz
⌘K
...
~/
/tutorials
/git/git-merge/edit
~ Contribute
Suggest a correction or improvement. The author reviews it before it goes live.
Loading...
Comment
0 / 300
Typo
Grammar
Broken link
Clarify
Code
en/tutorials/git/8git-merge
Write
Preview
Diff
# Merging Branches Merging combines the history of two branches into one. Git picks the best strategy automatically. ## Basic Merge ```bash git switch main git merge feature ``` This integrates `feature` into `main`. ## Fast-Forward Merge Happens when `main` has no new commits since `feature` was created: ``` main: A → B feature: A → B → C → D After merge: main: A → B → C → D ``` Git simply moves the pointer forward — no extra commit needed. ## Three-Way Merge Used when both branches have new commits: ``` main: A → B → E feature: A → B → C → D After merge: main: A → B → E ─┐ feature: C → D → M (merge commit) ``` Creates a new merge commit `M` combining both histories. ## No Fast-Forward Force a merge commit even for fast-forward cases: ```bash git merge --no-ff feature ``` Useful for preserving branch history in your log. ## Best Practices - Merge frequently to avoid large conflicts - Use `--no-ff` for feature branches to keep history clear - Always run tests before merging into `main` - Review the diff before completing a merge ## Common Mistakes - Merging into the wrong branch - Forgetting to commit or stash changes before merging - Not pulling latest `main` before merging - Leaving merge conflicts unresolved
No changes yet
Reset to original
Submit suggestion
cancel