Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Last updated: Monday 09 November 2026 @ 12:56:44

Git Branching Model

Git · Collaborative development

Branching, merging and rebasing

Develop changes safely on short-lived branches and examine how Git integrates their history.

GitBranchesHistoryPull requests
Isolate changes

Create a feature branch without disrupting main.

Integrate commits

Compare fast-forward, merge-commit and rebase workflows.

Inspect history

Use a graph and tags to explain how a feature reached the mainline.

The model

The original branching model has three principles: maintain a deployable mainline, work in feature branches, and review changes before integration. The original diagrams use master; this practical uses main consistently. They represent the same role, not two required branches.

Feature branches are integrated into a stable main branch

Feature branches feed changes back to a maintained mainline.

Practical: create and integrate a feature

  1. Create a repository and make an initial commit. Set the identity locally so you do not alter configuration for other repositories.

    mkdir -p ~/git-branch-lab && cd ~/git-branch-lab
    git init -b main
    git config user.name "Git Student"
    git config user.email "student@example.invalid"
    printf '# Sensor notes\n' > README.md
    git add README.md
    git commit -m "docs: create sensor notes"
  2. Create a feature branch and record a small change.

    git switch -c feature/adc-notes
    printf '\n## ADC\nAnalogue input converts voltage into a digital value.\n' >> README.md
    git diff
    git add README.md
    git commit -m "docs: explain ADC"
  3. Inspect the current branch and the commit graph.

    git branch -v
    git log --all --oneline --decorate --graph
  1. Switch to main and commit a separate documentation change.

    git switch main
    printf 'A small embedded systems project.\n' > PURPOSE.md
    git add PURPOSE.md
    git commit -m "docs: add project purpose"
  2. Display the graph again. Both branches now contain commits that the other does not.

    git log --all --oneline --decorate --graph
  3. Integrate the feature through a merge commit.

    git merge --no-ff feature/adc-notes -m "merge: ADC notes"
    git log --all --oneline --decorate --graph
    cat README.md

Compare rebase and merge

  1. Create another feature branch and make one commit.

    git switch -c feature/pwm-notes
    printf '\n## PWM\nDuty cycle controls the proportion of time a signal is high.\n' > PWM.md
    git add PWM.md
    git commit -m "docs: add PWM notes"
  2. Advance main independently.

    git switch main
    printf '\n## Team convention\nFeatures are reviewed before merging.\n' > CONTRIBUTING.md
    git add CONTRIBUTING.md
    git commit -m "docs: add contribution policy"
  3. Rebase the unpublished feature onto the latest main, then fast-forward merge it.

    git switch feature/pwm-notes
    git rebase main
    git switch main
    git merge --ff-only feature/pwm-notes
    git log --all --oneline --decorate --graph
  4. Tag the completed state and inspect the tag.

    git tag v0.1.0
    git show --stat v0.1.0

Choosing an integration strategy

MethodCommit historyUseful when
git merge --ff-onlyLinear; fails if the branches divergeThe feature has already been rebased or has not diverged
git merge --no-ffPreserves a distinct merge commitThe team wants an explicit integration point
git rebase mainReplays feature commits onto the current mainlinePreparing an unpublished feature for integration
Pull requestReview and checks before an integration policy is appliedWorking in GitHub or a similar hosting service
Comparison of readable and difficult-to-follow merge histories

Merge topology can make a feature's history easier or harder to investigate.

Further reading