🚧 Under construction — VoTechWorks is being built. A free tech program — complimentary, as our commitment to get America working.
voTechWorksGrade 13 · a vocational technology learning center, by urbanicity
newSchoolWorks.com › VoTechWorks › Technology & Design › Web & Software Development › Git Version Control for Web & Software Development › Lesson 5
TEC-WEB46-05 · Lesson 5 · 18 clock hours · lab

Branching: Creating, Switching, and Merging

Objective. Students create and switch between branches, perform fast-forward and three-way merges, and delete branches after successful integration.

⚠ Pending professional review

This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.

VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.

What Is a Branch?

A Git branch is a lightweight, movable pointer to a specific commit in your repository's history. When you create a branch, Git simply creates a new pointer — it does not copy files or duplicate work. This makes branching in Git very fast and inexpensive compared to older version-control systems.

Branching lets you isolate work. You can develop a new feature, fix a bug, or experiment with a design idea without touching the code that is already working on `main`. When the work is ready, you bring it back through a merge.

Creating and Switching Branches

The essential commands are:

```

git branch feature/navigation # creates the branch

git switch feature/navigation # moves HEAD to it

```

Or in a single step:

```

git switch -c feature/navigation

```

The older equivalent is `git checkout -b feature/navigation`. Both forms work; `git switch` is the modern, purpose-built command recommended for day-to-day use.

`HEAD` is Git's pointer to the commit you are currently working on. When you switch branches, HEAD moves to the tip of that branch. Any new commits you make extend that branch, not the one you left.

Fast-Forward Merges

A fast-forward merge happens when the branch you are merging into (`main`) has not received any new commits since the feature branch was created. Git can simply move `main`'s pointer forward to the tip of the feature branch — no new merge commit is created.

```

git switch main

git merge feature/navigation

```

Git will print a message like `Fast-forward` and show which files changed. The history stays linear — clean and easy to read. This is the simplest and most common merge scenario.

Three-Way Merges

A three-way merge occurs when both branches have diverged — each has at least one commit the other does not share. Git identifies three points:

1. The common ancestor commit (where the branches split).

2. The tip of the current branch (`main`).

3. The tip of the branch being merged in.

Git combines the changes and automatically creates a merge commit — a commit with two parents. The resulting history is not linear; it shows the fork and the point of reunification. You can visualize this with:

```

git log --graph --oneline --all

```

The `--graph` flag draws ASCII art of the branch topology, `--oneline` keeps it compact, and `--all` shows every branch, not just the current one.

Merge Conflicts

If both branches edited the same line of the same file, Git cannot resolve the difference automatically. It pauses the merge and marks the file with conflict markers (`<<<<<<<`, `=======`, `>>>>>>>`). You must open the file, decide which version to keep (or combine them), remove the markers, stage the resolved file with `git add`, and complete the merge with `git commit`. This is normal workflow — not an error in Git.

Deleting Branches

After a successful merge, the feature branch is no longer needed. Delete it to keep the repository tidy:

```

git branch -d feature/navigation

```

The `-d` flag is safe: Git refuses to delete a branch that has not been fully merged into its upstream or the current branch, and prints a warning telling you so. This protects work you might otherwise lose.

If you are certain you want to discard an unmerged branch — for example, a prototype you have decided to abandon — use the force-delete flag:

```

git branch -D feature/abandoned-idea

```

Use `-D` deliberately and sparingly. Once the pointer is deleted and the commits are not referenced anywhere else, Git's garbage collector will eventually remove them. Document your reasoning in the lab log whenever you force-delete.

Safety and Professional Practices

Branching is a key safety practice in professional development. Never commit directly to `main` in a team environment without a review process. All new work should live on a dedicated branch so that `main` always reflects a stable, known-good state.

In workplace settings, branches are often protected by repository rules (on platforms such as GitHub or GitLab) that require a pull request and at least one reviewer before merging. Understanding local branching mechanics is the foundation for participating in those workflows.

Before switching branches, confirm that your working directory and staging area are clean (`git status`). Uncommitted changes can follow you to the new branch or, in some cases, prevent the switch entirely. Always commit or stash your work before switching.

Reading the Repository Graph

After completing both merges in this lab, `git log --graph --oneline --all` will show two distinct shapes:

Being able to read this graph fluently is an essential skill. It tells you the full story of your project's development history at a glance.

Key terms8

BranchA lightweight, movable pointer to a commit in Git; used to isolate work such as a new feature or bug fix from the main codebase.
HEADGit's special pointer that indicates the commit (and usually the branch) you are currently working on; moves when you switch branches or make new commits.
Fast-forward mergeA merge in which Git simply moves the target branch pointer forward to the tip of the source branch because no diverging commits exist; no merge commit is created.
Three-way mergeA merge that occurs when both branches have unique commits since they diverged; Git uses the common ancestor and both branch tips to produce a new merge commit with two parents.
Merge commitA commit created during a three-way merge that has two parent commits, recording the point where two lines of history were combined.
Merge conflictA situation where both branches changed the same part of the same file and Git cannot automatically determine which version to keep; the developer must resolve it manually.
git branch -dThe safe branch-deletion flag; Git refuses to delete a branch that has not yet been merged, protecting unmerged work from accidental loss.
git branch -DThe force-delete flag that removes a branch regardless of its merge status; use deliberately when you intentionally want to discard unmerged commits.
In class: Starting from a provided repository representing a small website, create a feature branch named feature/navigation, add a navigation bar to the HTML, commit the change, then merge it into main using git merge and observe whether a fast-forward or a merge commit occurs. · Create a second branch named feature/footer from main, commit changes to the footer file, then switch to main and commit an unrelated change to a different file; merge feature/footer into main to trigger a three-way merge commit and examine the resulting graph with git log --graph --oneline --all. · Delete merged branches with git branch -d and attempt to delete an unmerged branch with git branch -d to observe the safety warning; document the difference between -d and -D flags in the lab log with a note on when force-delete is appropriate.
Materials: Git workstation, provided starter repository, terminal, VS Code, lab log, Pro Git book chapter 3
Check: Instructor inspects repository graph showing correct fast-forward and three-way merge commits; lab log entry complete; student verbally explains the difference between the two merge types to instructor satisfaction.

Practice check8

Answer each question, then check it. Nothing is recorded; this is your own practice.

Version control and software development fundamentals
1. You run `git merge feature/navigation` from `main` and Git prints 'Fast-forward'. What does this indicate?
Version control and software development fundamentals
2. Which command creates a new branch called `feature/footer` AND immediately switches to it in one step, using the modern Git syntax?
Version control and software development fundamentals
3. After merging `feature/footer` into `main`, you run `git branch -d feature/footer`. What will Git do?
Version control and software development fundamentals
4. You try `git branch -d feature/experiment` but the branch has never been merged. What does Git do?
Version control and software development fundamentals
5. A three-way merge requires Git to examine how many distinct commits?
Version control and software development fundamentals
6. Which `git log` invocation will display the full branch graph for all branches in a compact format?
Version control and software development fundamentals
7. During a three-way merge, Git stops and marks files with `<<<<<<<` and `>>>>>>>`. What is the correct next action?
Version control and software development fundamentals
8. A colleague insists on committing all new features directly to `main` without using branches. Which professional risk does this create?
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.