Git Branching Model
Branching, merging and rebasing
Develop changes safely on short-lived branches and examine how Git integrates their history.
Create a feature branch without disrupting main.
Compare fast-forward, merge-commit and rebase workflows.
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 feed changes back to a maintained mainline.
Practical: create and integrate a feature
-
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" -
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" -
Inspect the current branch and the commit graph.
git branch -v git log --all --oneline --decorate --graph
-
Switch to
mainand 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" -
Display the graph again. Both branches now contain commits that the other does not.
git log --all --oneline --decorate --graph -
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
-
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" -
Advance
mainindependently.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" -
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 -
Tag the completed state and inspect the tag.
git tag v0.1.0 git show --stat v0.1.0
Choosing an integration strategy
| Method | Commit history | Useful when |
|---|---|---|
git merge --ff-only | Linear; fails if the branches diverge | The feature has already been rebased or has not diverged |
git merge --no-ff | Preserves a distinct merge commit | The team wants an explicit integration point |
git rebase main | Replays feature commits onto the current mainline | Preparing an unpublished feature for integration |
| Pull request | Review and checks before an integration policy is applied | Working in GitHub or a similar hosting service |

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