Beginner30 minutesGitLevel 2

Git Branching

Learning Objectives

By the end of this lesson, you'll be able to:

  • Understand Understand what branches are and why they are useful
  • Create Create and switch between branches
  • Merge branches to combine changes
  • Resolve merge conflicts

Why This Matters:

Branching is how professional teams work on features, fix bugs, and experiment—all without breaking the main codebase. It's a skill you'll use every day.

Before You Start:

You should be familiar with:

  • Be familiar with basic Git commands
  • Have a Git repository ready to practice with

What is a Branch?

A branch in Git is like a parallel version of your project. It allows you to work on new features, bug fixes, or experiments without affecting the main codebase.

Think of branches as alternate timelines in your project. You can create a branch, make changes, and then merge it back into the main timeline when you're ready.

Step 1: Create a Branch

To create a new branch, use the following command:

Replace <branch-name> with the name of your branch. For example:

This creates a new branch called feature-login, but it does not switch to it yet.

Step 2: Switch to a Branch

To switch to a branch, use the following command:

For example:

This switches your working directory to the feature-login branch. Any changes you make will now be saved to this branch.

Step 3: Merge a Branch

Once you've finished working on a branch, you can merge it back into the main branch (usually called main or master). First, switch to the branch you want to merge into:

Then, merge the branch using:

For example:

This combines the changes from feature-login into the main branch.

Step 4: Resolve Merge Conflicts

Sometimes, Git cannot automatically merge changes because the same lines of code were modified in both branches. This is called a merge conflict.

Git will mark the conflicting files and ask you to resolve the conflicts manually. Open the files, look for the conflict markers (e.g., <<<<<<<), and decide which changes to keep.

After resolving the conflicts, stage the changes and complete the merge:

Try It Yourself

Use the interactive terminal below to practice creating, switching, and merging branches:

Setting Up Git in VS Code

Visual Studio Code has built-in Git support, making it easy to manage your repositories directly from the editor. Follow these steps to set up Git in VS Code:

Step 1: Open the Source Control Panel

Click on the Source Control icon in the Activity Bar on the left side of the editor. If you have a Git repository initialized in your project folder, it will automatically appear here.

Step 2: Configure Git in VS Code

If you haven't already configured Git globally, VS Code will prompt you to set your name and email. You can also configure it manually by running the following commands in the integrated terminal:

Step 3: Commit Changes

To commit changes, stage your files by clicking the icon next to the file name in the Source Control panel. Then, enter a commit message in the text box and click the checkmark icon to commit.

Step 4: Push and Pull Changes

Use the Source Control panel to push your changes to a remote repository or pull changes from it. You can also use the integrated terminal to run git push and git pull commands.

Step 5: View Git History

Install the Git History extension to view your commit history in a visual format. This can be helpful for tracking changes and understanding your project's timeline.

🏁 Lesson Complete: Git Branching

Key Takeaways:

  • Branches let you work on features or fixes without affecting the main codebase
  • Use git branch to create branches and git checkout (or git switch) to switch between them
  • git checkout -b creates and switches to a new branch in one step
  • Merging combines changes from one branch into another
  • Merge conflicts happen when the same lines change in both branches and must be resolved manually

Learning Objectives Review:

Look back at what you set out to learn. Can you now:

  • ✅ Understand what branches are and why they are useful Check!
  • ✅ Create and switch between branches Got it!
  • ✅ Merge branches to combine changes Can explain it!
  • ✅ Resolve merge conflicts Could teach this!

If you can confidently answer "yes" to most of these, you're ready to move on!

Think & Reflect:

💭 💭 Reflection Questions

  • When would you create a new branch instead of committing directly to main?
  • What naming convention would you use for your branches?
  • How does branching make team collaboration easier?

🎯 Looking Ahead:

Now that you understand branching, the next step is to learn about merging—how to combine branch changes and handle conflicts.

Recommended Next Steps

Continue Learning

Ready to move forward? Continue with the next tutorial in this series:

Merging

Related Topics

Explore these related tutorials to expand your knowledge:

Practice Projects

Apply what you've learned with these hands-on projects:

Branching Exercise

Create, switch, and merge branches in a sample repo.

gitbranchmerge
Start Project

Additional Resources

Deepen your understanding with these helpful resources:

Progress tracking is disabled. Enable it in to track your completed tutorials.