Merging Branches (git merge)
π‘ Intermediate
π Definition
git merge is the command used to integrate independent commit histories from a source branch (e.g. feature-login) into a target branch (e.g. main), combining code modifications into a single unified timeline.
π Multilingual Explanation
English
git merge joins two branch histories. To merge feature into main, you must first switch to main (git switch main) and then execute git merge feature. If main has no new commits since the branch split, Git performs a Fast-Forward Merge (simply moving the branch pointer). If both branches have diverged, Git performs a 3-Way Merge and generates an automatic Merge Commit.
Hindi
Feature branch ka kaam poora hone par use main branch mein milane ke liye git merge ka use hota hai. Pehle target branch (main) par switch karein (git switch main), phir git merge feature-name run karein. Agar donon branches mein alag-alag commits huye hain toh Git ek 3-Way Merge Commit banata hai.
Marathi
Feature branch madhil kaam main branch madhye aannyasathi git merge vaparatat. Pehle git switch main karun main branch var jaave, mag git merge feature-name command chalvavi.
Hinglish
feature branch se code main branch mein combine karne ke liye git merge use hota hai. Rule simple hai: Hamesha us branch par khade hokar merge command chalayein jisme code AANA chahiye. (e.g., main branch par jaakar git merge feature run karein).
π€ Why Do We Use It?
Branching isolates development, but completed features must eventually be published to the main production codebase so users can access the new functionality. Merging brings those changes together.
π§ Simple Explanation
Think of merging like two streams of a river joining together:
mainis the primary river stream.featureis a side canal that split off to collect new water.- At the end of the feature project, the canal flows back into the primary river (Merge), making the river fuller and complete.
π The 2 Primary Merge Types
1. Fast-Forward Merge
Occurs when the main branch has received NO new commits since the feature branch split off. Git simply moves the main branch pointer forward to match the feature commit. No extra merge commit is created.
Before Merge:
[main] ββ> C1 ββ> C2
βββ> C3 ββ> C4 [feature]
After Fast-Forward Merge (`git merge feature` on `main`):
[main] ββ> C1 ββ> C2 ββ> C3 ββ> C4 [feature]
2. 3-Way Merge
Occurs when BOTH main and feature have received independent commits since splitting (diverged history). Git compares the common ancestor commit, mainβs latest commit, and featureβs latest commit, generating a special Merge Commit with two parent commits.
Before Merge:
[main] ββ> C1 ββ> C2 ββ> C5 [main]
βββ> C3 ββ> C4 [feature]
After 3-Way Merge:
[main] ββ> C1 ββ> C2 ββ> C5 βββββββββ> C6 (Merge Commit) [main]
βββ> C3 ββ> C4 βββββββ
π‘ Practical Example
Complete step-by-step terminal merging workflow:
# 1. Switch to destination branch (main)
git switch main
# 2. Ensure main is up to date
git pull origin main
# 3. Execute merge from feature branch
git merge feature-dark-mode
# 4. (Optional) Delete feature branch after successful merge
git branch -d feature-dark-mode
π Command Breakdown
git switch main: Ensures you are standing on the receiving branch.git merge feature-dark-mode: Pulls changes FROMfeature-dark-modeINTO current active branchmain.--no-ff: Flag forcing Git to create a 3-Way Merge Commit even if a Fast-Forward merge is possible (preserves visual feature history).
π Terminal Output
# Fast-Forward Output:
$ git merge feature-dark-mode
Updating a1b2c3d..e5f6g7h
Fast-forward
dark.css | 5 +++++
1 file changed, 5 insertions(+)
create mode 100644 dark.css
β οΈ Common Mistakes
- Merging from Wrong Direction: Standing on
featurebranch and runninggit merge mainwhen you intended to mergefeatureintomain! Always verify active branch withgit branchbefore merging. - Panic on Merge Conflicts: Getting scared when text conflicts occur. (Merge conflicts are normal and covered in the next lesson!).
π‘οΈ Safety / Important Notes
- If a 3-way merge is initiated and you want to cancel it safely before committing:
git merge --abort
π Real-World Usage
GitHub Pull Requests execute git merge automatically when an engineering lead clicks the green βMerge Pull Requestβ button on the GitHub website interface.
π§ͺ Try It Yourself
- In your
git-practicerepo, switch tomainusinggit switch main. - Merge your earlier
feature-signupbranch by runninggit merge feature-signup. - Observe whether Git performed a Fast-Forward merge or 3-Way merge.
π― Mini Challenge
Run git log --oneline --graph --all after merging to visually inspect how the commit history nodes connected together.
π Related Topics
π§ Navigation
| β Home | β Previous: Branching | Next: Merge Conflicts β |