Git Branch Tutorial šŸŽÆ

beginner
19 min

Git Branch Tutorial šŸŽÆ

Welcome to our comprehensive Git Branch tutorial! In this lesson, we'll explore the power of Git branches, learning how to create, switch, merge, and delete branches to manage your projects efficiently.

What is a Git Branch? šŸ“

A Git branch is a separate line of development in a Git repository. It allows you to work on different features, fixes, or experiments independently without affecting the main project.

Why use Git Branches? šŸ’”

  • Isolate changes: Work on a new feature without disrupting the existing project.
  • Collaboration: Team members can work on different parts of the project simultaneously.
  • Easier testing and deployment: Test new features separately before merging them into the main project.

Creating a New Branch šŸŽØ

Let's create a new branch named feature-branch:

bash
$ git branch feature-branch

Now, let's switch to the new branch:

bash
$ git checkout feature-branch

šŸ“ Note: By default, Git creates branches based on the current HEAD, so feature-branch will start from the same commit.

Making Changes in a Branch šŸ‘©ā€šŸ’»

Now, you can make changes in your project as usual. Let's add a new file new-file.txt and commit the changes:

bash
$ touch new-file.txt $ git add new-file.txt $ git commit -m "Adding new file"

Viewing Branches 🌳

To see all branches, use the following command:

bash
$ git branch

You should see both master and feature-branch.

Merging Branches šŸ”„

When your feature is complete, you can merge feature-branch into master:

bash
$ git checkout master $ git merge feature-branch

Deleting Branches šŸ—‘ļø

After merging, you can delete the feature-branch:

bash
$ git branch -d feature-branch

Handling Conflicts 🚧

When merging branches, conflicts may occur. Resolve them manually and then commit the merge again.

Quick Quiz
Question 1 of 1

Which command creates a new Git branch?

Quick Quiz
Question 1 of 1

How do you switch to a different branch in Git?