Branching and Merging

Now, we will explore one of Git's most powerful features: branching. We will also learn how to merge these branches back together.

What is a Branch?

Think of your project as a tree. The main branch (usually called main in projects) is like the tree's trunk. It's the core of your project, the stable version from which everything else grows.

Now, imagine you want to add a new feature to your software. You wouldn't want to experiment directly on the stable version, right? That's where branches come in. A branch is like a new limb growing out from the trunk. It allows you to work on your new feature in isolation without affecting the main codebase.

Here's why branches are so helpful:

  1. Parallel Development: Multiple developers can work on different features simultaneously without interfering with each other.

  2. Experimentation: You can try out new ideas without risking the stability of your main codebase. If the experiment fails, you can delete the branch.

  3. Organized Workflow: Branches help organize work. You might have a branch for each feature, bug fix, or version of your software.

  4. Code Review: When a feature is complete, you can create a "pull request" (we'll cover this in a later section) to merge your branch into the main branch. This allows for code review before changes are integrated.

A branch in Git is essentially a separate line of development and your code begins wherever you "branch". It allows you to work on This is incredibly useful when you're working on a team, or when you want to try out new ideas without risking your stable code.

Creating and Switching Branches

Let's get hands-on with branches. We'll continue using the repository we created in the last chapter.

First, let's see what branch we're currently on:

git branch

You should see * main, indicating you're on the main branch (note: older Git versions may call this 'master' instead of 'main').

Now, let's create a new branch:

git branch feature-login

This creates a new branch called feature-login.

To switch to this new branch, use:

git checkout feature-login

There's a shortcut to create and switch to a new branch in one command:

git checkout -b feature-signup

This creates a new branch called feature-signup and switches to it.

To see all branches, use:

git branch

You should see all your branches listed, with an asterisk next to your current branch.

Making Changes in a Branch

Now that we're in a new branch, let's make some changes.

Create a new file:

echo "This is a new feature" > new_feature.txt

Stage and commit this change:

git add new_feature.txt
git commit -m "Add new feature"

Switch back to the main branch:

git checkout main

Look for your new file:

ls

You won't see new_feature.txt because it only exists in the feature-signup branch!

Merging Branches

Now that we've made changes in our feature branch, if you are happy with them, we want to merge them back into our main branch.

Make sure you're on the branch you want to merge into (in this case, main):

git checkout main

Merge the feature branch into main:

git merge feature-signup

If there are no conflicts, Git will automatically create a new commit with the changes from feature-signup. If there are conflicts (don't worry, we'll cover that next), Git will ask you to resolve them manually. Conflicts happen when Git can't figure out how to merge the code.

Handling Merge Conflicts

Sometimes, you might change the same part of a file in two different branches. When this happens, Git can't automatically determine which change to keep. This is called a merge conflict.

Let's create a conflict and resolve it:

Create a new branch and switch to it:

git checkout -b conflict-branch

Edit the hello.txt file, add the line "This will conflict", and commit the change.

Switch back to main:

git checkout main

Edit the same hello.txt file, add a different line "This will also conflict", and commit the change.

Now try to merge conflict-branch into main:

git merge conflict-branch

Git will tell you there's a conflict and ask you to resolve it manually. Open hello.txt, and you'll see something like this:

<<<<<<< HEAD
This will also conflict
=======
This will conflict
>>>>>>> conflict-branch

Edit the file to resolve the conflict, removing the conflict markers and keeping the content you want.

After editing, stage the resolved file:

git add hello.txt

Complete the merge by creating a commit:

git commit -m "Resolve merge conflict"

Merge conflicts can often terrify folks, but once you know what changes you need, they're easy to fix. Well done on fixing your first conflict!

In the next chapter, we'll explore using Git for remote collaboration and introduce concepts like pushing, pulling, and working with GitHub.

GitBeginner
Avatar for Niall Maher

Written by Niall Maher

Founder of Codú - The web developer community! I've worked in nearly every corner of technology businesses: Lead Developer, Software Architect, Product Manager, CTO, and now happily a Founder.

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.