Computer Science Grade 9 20 min

5. Understanding Git Branches

Introduce the concept of branches in Git and their use for parallel development.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define what a Git branch is and explain its purpose in software development. Explain the role of the `main` branch as the source of truth. Create a new branch using the `git branch` command. Switch between different branches using the `git checkout` command. Create and switch to a new branch in a single step. Make and commit changes on a separate feature branch, isolating work from the `main` branch. List all local branches in their repository. Ever wanted to try a risky new idea in your code without breaking the working version? 🤔 What if you could have a 'save point' and a 'what-if' version at the same time? In this lesson, you'll learn about Git branches, which are like parallel timelines for your code. This powerful featur...
2

Key Concepts & Vocabulary

TermDefinitionExample BranchA separate line of development in a Git repository. It's like making a copy of your project at a specific point in time where you can work independently without affecting other versions.If your main project is a story, a branch could be a new chapter you're drafting. You can write and edit it without changing the main story until you're ready. main BranchThe default branch in a Git repository, often considered the official, stable, and working version of the project. New features are eventually merged into this branch.In a website project, the `main` branch holds the code for the live website that users see. You want to keep this branch clean and functional at all times. HEADA special pointer in Git that points to the current branch you are worki...
3

Core Syntax & Patterns

Create a New Branch git branch <branch-name> Use this command to create a new branch. This command creates a new pointer to your current commit but does NOT switch you to the new branch. Switch to an Existing Branch git checkout <branch-name> Use this command to switch your working directory to a different, existing branch. The HEAD pointer will move to this branch. Create and Switch (Shortcut) git checkout -b <new-branch-name> A convenient shortcut that combines `git branch` and `git checkout`. It creates the new branch and immediately switches to it, so you can start working right away.

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
You are on `main`. You run `git checkout -b feature-A`, make a commit, then run `git checkout main`, and finally run `git checkout -b feature-B`. How are the commit histories of `feature-A` and `feature-B` related?
A.`feature-B` contains the new commit from `feature-A`.
B.`feature-A` and `feature-B` are separate lines of development that both branch off from the same commit on `main`.
C.`feature-A` is now based on `feature-B`.
D.Both branches are empty and have no relation to `main`.
Challenging
While on the `main` branch, a student runs `git branch new-design`. They then, without switching branches, modify `style.css` and run `git commit -m "Update design"`. Finally, they run `git checkout new-design`. What will the `style.css` file look like on the `new-design` branch?
A.It will contain the new design changes.
B.It will be identical to the old version, before the 'Update design' commit.
C.The file will be deleted on the `new-design` branch.
D.The file will show a merge conflict.
Challenging
The tutorial provides two examples: adding a new 'About Us' page and experimenting with a CSS color change. What core principle of branching do both of these examples demonstrate?
A.That branches should only be used for adding new files, not for modifying existing ones.
B.That branches are primarily for fixing bugs in the `main` branch.
C.The principle of isolation: any type of work, big or small, can be safely developed on a branch without impacting the main codebase.
D.The principle that every new branch must be named after the file being changed.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from III. Introduction to Version Control with Git

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.