Stage 4 - Branching and Merging
In Stage 3 we made the codebase cleaner: only intended edits reached commit. Now the team wants a new feature: show task priorities with color labels in task-service. You do not do this directly on main. You create a branch.

Branches are safe working threads
A branch is a separate place to work on one idea without breaking the stable version of the project. Keep main as the version that should stay usable. Create a branch when you want to try a feature, fix a bug, or prepare a change for review.
Think of it like this: main is the clean team version, and your branch is your workbench. You can make commits on the branch, check the result, and only then bring the change back to main.
Open your project state and create a feature branch:
git switch main
git switch -c feature/task-priority-labels
Now task-service.js can evolve without disturbing the baseline. Open the file in your editor, replace its content with this version, and save it:
const tasks = [];
export function addTask(task) {
const exists = tasks.some((item) => item.title === task.title);
if (exists) return false;
tasks.push(task);
return true;
}
Commit this feature separately:
git add task-service.js
git commit -m "feat: prevent duplicate task titles"
Returning and merging
Switch back to stable line and merge:
git switch main
git merge feature/task-priority-labels
If main has not changed on the same files, merge is often clean or fast-forward. If it changed, Git may create a merge commit. Either result is valid as long as the feature behavior is now in main.
| Command | Use when | Key idea |
|---|---|---|
git branch |
show branch list | Names represent work streams |
git switch -c |
create + switch in one command | Use short, descriptive names |
git merge |
bring branch into current branch | “Current branch receives changes” |
Branching in team rhythm
Instead of one giant commit for everything, teams usually create multiple branches:
fix/duplicate-checkfeature/task-priority-labelsdocs/task-readme-refresh
Each branch carries one intent. That makes review faster and rollback safer.
Extended reflection: from branch behavior to release safety
When a branch is rejected by team review, Git does not force you to delete effort. You can keep the branch, add one correction commit, and try merge again. This changes nothing in main until acceptance. That makes branching safer than editing main directly, especially when business rules evolve quickly.
As a long-lived habit, keep at least two local references:
- the current feature branch,
- a clean
mainbranch kept aligned with remote.
Before every merge, run a quick git log --oneline --graph check and confirm that your branch history is coherent and small. Teams that follow this process detect accidental overlap earlier, because they can compare your branch path against the shared main line without opening every file.
At this point your project workflow should have both a shared future and a recoverable past.
Glossary
- version control
- A system for tracking project changes over time so developers can inspect, compare, and restore previous states.
- repository
- A storage area for a project, its files, history, and related metadata.
- commit
- A saved Git history point that records a related set of changes, the author, and the reason for the change.
- branch
- An independent line of work where changes can be developed without immediately affecting another line.
- merge
- The operation that combines changes from one branch or source into another.
- conflict
- A situation where a tool cannot automatically combine changes because different edits affect the same part.
- staging area
- The Git area where selected changes are prepared before they become part of the next commit.
- working tree
- The current files and folders checked out in your project directory, including uncommitted edits.
- diff
- A comparison that shows exactly which lines or values were added, removed, or changed.
- remote
- A named connection to a repository stored somewhere else, usually on a server such as GitHub.
- clone
- The operation that copies an existing repository from a remote location to your local machine.
- push
- The operation that sends local commits or changes to a remote repository.
- pull
- The operation that downloads changes from a remote repository and integrates them locally.
- pull request
- A review request that proposes merging one branch into another and gives teammates a place to discuss changes.
- fork
- A personal copy of another repository, usually used to propose changes without direct write access.
- review
- The engineering practice of checking changes for correctness, clarity, risks, and maintainability.
- rollback
- A deliberate return to an earlier safe state after a mistake, regression, or unwanted change.
- rebase
- A Git operation that moves commits to a new base commit, often used to make history more linear.
- tag
- A named marker pointing to a specific version, commit, image, or release artifact.