Git Checkout (Branch) Tutorial 🎯

beginner
24 min

Git Checkout (Branch) Tutorial 🎯

Welcome to our Git Checkout (Branch) tutorial! In this lesson, you'll learn how to work with branches in Git, a powerful tool for managing code changes. Let's dive in! 💡

What is a Git Branch? 📝

A Git branch is a separate line of development, allowing you to work on different features or fixes without affecting the main project. Think of it as a detour on a road where you can work on something new without blocking the main route.

Creating a New Branch 🎯

To create a new branch, use the command:

bash
git branch <branch-name>

For example:

bash
git branch feature-branch

This command creates a new branch named feature-branch but doesn't switch to it yet.

Switching to a Branch 🎯

To switch to a newly created branch, use the command:

bash
git checkout <branch-name>

Now, let's create and switch to a new branch:

bash
git branch feature-branch git checkout feature-branch

Now you're working on the feature-branch.

Making Changes on a Branch 💡

Now that you're on the feature-branch, you can make changes to your heart's content without affecting the main project.

Saving Changes 💡

Once you've made changes, you need to save them. Use the commands:

bash
git add <file> git commit -m "Your commit message"

For example:

bash
git add index.html git commit -m "Added feature-branch"

Merging a Branch 💡

When you're ready to merge your changes into the main project, switch back to the main branch:

bash
git checkout master

Then, merge the feature-branch into master:

bash
git merge feature-branch

Resolving Conflicts 💡

In case of conflicts between the branches, Git will alert you. You'll need to resolve these conflicts manually before you can successfully merge the branches.

Deleting a Branch 💡

To delete a branch, switch to the branch you want to delete and then use the command:

bash
git branch -d <branch-name>

For example:

bash
git checkout feature-branch git branch -d feature-branch

Quiz 📝

Quick Quiz
Question 1 of 1

What command creates a new Git branch?

Quick Quiz
Question 1 of 1

How do you switch to a new Git branch?

Quick Quiz
Question 1 of 1

How do you merge a Git branch into the main branch?