Pages

GIT: Chapter 6 — Branching and Merging

Chapter 6 — Branching and Merging


6.1 Introduction

Branching is one of Git’s most powerful capabilities. It enables parallel development without interference, allowing multiple features, fixes, and experiments to progress simultaneously.

Merging is the process of integrating changes from one branch into another, forming a unified code history.

This chapter examines Git’s branching model, branch manipulation commands, merging strategies, conflict resolution, and best-practice workflows.


6.2 Concept of Branching

A Git branch is essentially a movable pointer to a commit.

  • The default branch is typically main (historically master)

  • Each branch represents an independent line of development

  • Branch creation is lightweight because Git stores snapshots rather than file copies

Commit graph abstraction

A — B — C (main)
\
D — E (feature)

Branch pointers:

  • main → C

  • feature → E


6.3 HEAD Pointer

HEAD represents the current checkout position.

  • Normally points to a branch

  • Can point directly to a commit (detached HEAD)

Inspect HEAD

git log --oneline

Current commit corresponds to HEAD.


6.4 Creating Branches

Create branch

git branch feature-login

Creates branch without switching.


Create and switch

git checkout -b feature-login

Modern equivalent:

git switch -c feature-login

6.5 Listing Branches

git branch

Output example:

* main
feature-login

* denotes active branch.


Remote branches

git branch -r

All branches

git branch -a

6.6 Switching Branches

git switch feature-login

Legacy command:

git checkout feature-login

Switching updates working directory to match branch snapshot.


6.7 Renaming Branches

git branch -m old-name new-name

Used for standardization or workflow restructuring.


6.8 Deleting Branches

Safe deletion

git branch -d feature-login

Prevents deletion if unmerged.


Force deletion

git branch -D feature-login

Removes regardless of merge state.


6.9 Merging Fundamentals

Merging integrates histories.

Basic merge

git switch main
git merge feature-login

Possible outcomes:

  • Fast-forward merge

  • Three-way merge

  • Merge conflict


6.10 Fast-Forward Merge

Occurs when target branch has no new commits.

A — B — C (main)
\
D — E (feature)

After merge:

A — B — C — D — E (main)

Git simply moves pointer.


6.11 Three-Way Merge

Occurs when both branches diverge.

D — E (feature)
/
A — B — C (main)

Git identifies common ancestor and creates merge commit.

Result:

A — B — C — M
\ /
D — E

6.12 Merge Commit Characteristics

  • Multiple parents

  • Represents integration point

  • Preserves parallel history

Inspect via:

git log --graph

6.13 Merge Conflicts

Conflicts occur when:

  • Same file

  • Same lines

  • Divergent changes

Conflict markers

<<<<<<< HEAD
main code
=======
feature code
>>>>>>> feature-login

Resolution steps:

  1. Edit file manually

  2. Remove markers

  3. Stage file

  4. Commit merge


6.14 Conflict Resolution Workflow

git merge feature-login
# conflict occurs
# edit files
git add resolved-file
git commit

Git finalizes merge commit.


6.15 Abort Merge

git merge --abort

Restores pre-merge state.


6.16 Merge Strategies

Recursive (default)

  • Handles complex history

Ours

  • Keeps current branch content

git merge -s ours branch

Octopus

  • Multi-branch merge

Subtree

  • External project integration


6.17 Rebase vs Merge

Rebase rewrites history; merge preserves it.

Merge

A — B — C — M
\ /
D — E

Rebase

A — B — C — D' — E'

Rebase creates linear history.


Rebase command

git rebase main

Use cautiously in shared branches.


6.18 Interactive Rebase

git rebase -i HEAD~3

Capabilities:

  • Squash commits

  • Reorder

  • Edit messages

  • Drop commits

Supports history refinement.


6.19 Cherry-Pick

Apply specific commit from another branch.

git cherry-pick commit-hash

Useful for:

  • Hotfix propagation

  • Selective integration


6.20 Branching Workflows

1. Feature branching

  • Each feature separate branch

  • Merge after completion

2. Git Flow

  • main

  • develop

  • feature

  • release

  • hotfix

Structured enterprise workflow.


3. GitHub Flow

Simplified:

  1. Branch

  2. Commit

  3. Pull request

  4. Review

  5. Merge


4. Trunk-based development

  • Short-lived branches

  • Frequent integration

  • CI-driven


6.21 Detached HEAD State

Occurs when checkout commit directly:

git checkout commit-hash

Implications:

  • No branch pointer movement

  • Commits may become orphaned

Create branch to preserve:

git switch -c temp

6.22 Best Practices

PracticeRationale
Short-lived branchesReduce conflicts
Frequent pullStay synchronized
Small mergesEasier review
Clear branch namingImprove collaboration
Rebase before mergeCleaner history

6.23 Practical Scenario

git switch -c feature-payment
# develop
git add .
git commit -m "payment module"
git switch main
git merge feature-payment
git branch -d feature-payment

Represents typical feature lifecycle.


6.24 Summary

This chapter covered:

  • Git branch architecture

  • HEAD pointer behavior

  • Branch creation, switching, deletion

  • Fast-forward and three-way merges

  • Conflict detection and resolution

  • Rebase mechanics

  • Cherry-picking

  • Industry workflows

Branching and merging are foundational to collaborative development. Proper application enables safe parallel work while preserving repository integrity.

No comments:

Post a Comment