Computer Science Grade 8 20 min

Branching and Merging

Branching and Merging

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Explain the purpose of creating a branch in version control. Create a new branch from the main project line. Switch between different branches in a project. Make and commit changes on a separate branch. Define what a merge is and perform a basic merge of one branch into another. Identify what a merge conflict is and describe why it happens. Ever wanted to add a cool new feature to your code but were scared of breaking everything that already works? 🤔 What if you could have a safe playground to experiment? This tutorial will teach you about branching and merging, two of the most powerful ideas in version control. You'll learn how to work on different versions of your project at the same time without causing chaos, just like professional software dev...
2

Key Concepts & Vocabulary

TermDefinitionExample BranchA separate, independent line of development in your project. Think of it as a copy of your project where you can make changes without affecting the original.If your main project is a story, creating a 'new-character' branch is like writing a draft of a new character's backstory on a separate piece of paper before adding it to the main story. Main Branch (or Master)The primary, official branch of your project. It usually contains the stable, working version of your code that is ready to be used.The 'main' branch is like the final, published version of a book. It's the one everyone reads and trusts. CheckoutThe action of switching from one branch to another. This changes the files in your project directory to match the version stored...
3

Core Syntax & Patterns

Creating a Branch git branch <branch-name> Use this command to create a new branch. It makes a copy of your current branch's state under a new name, but it does not switch you to it. Switching Branches git checkout <branch-name> Use this command to switch your working directory to a different branch. You can also use 'git checkout -b <branch-name>' to create and switch to a new branch in one step. Merging a Branch git merge <branch-name> First, use 'checkout' to switch to the branch you want to receive the changes (e.g., 'main'). Then, run this command with the name of the branch you want to bring in (e.g., 'feature-branch').

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 the `main` branch. You run `git merge feature-x` and a merge conflict occurs. After you manually edit the file to fix the conflict and remove the markers, what is the essential next step to complete the merge process?
A.Run `git merge feature-x` again.
B.Delete the `feature-x` branch.
C.Commit the resolved file.
D.Checkout the `feature-x` branch.
Challenging
Imagine the `main` branch has `app.js`. You create branch `feature-A` and add a new function to the end of `app.js`. Your friend on branch `feature-B` also adds a different new function to the end of `app.js`. You merge `feature-A` into `main` successfully. What is the most likely outcome when you then try to merge `feature-B` into `main`?
A.merge conflict will occur because both branches edited the same file.
B.The merge will fail, and you will have to re-do the work from `feature-B`.
C.The merge will likely succeed automatically, with both new functions appearing one after the other in `app.js`.
D.The function from `feature-B` will overwrite the function from `feature-A`.
Challenging
You are on your feature branch, `add-login`, and you realize the `main` branch has an important update you need. To get this update, you mistakenly run `git merge main` while still on the `add-login` branch. What has happened?
A.You have correctly updated your feature branch with the latest changes from main.
B.You have performed a 'reverse merge', bringing all of main's history into your feature branch.
C.You have pushed your incomplete login feature into the main branch.
D.The command fails because you cannot merge main into another branch.

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 Version Control

Ready to find your learning gaps?

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