2.1K Views

GIT105 - Working with Branches

Learn how Git branches work, how to create and switch branches, and how to merge changes effectively in your projects.

Branching is one of Git's most powerful features.
Branches allow developers to isolate work, experiment safely, and collaborate without interrupting the main codebase.

What Is a Branch?

A branch is a lightweight pointer to a specific commit.
It enables safe experimentation and parallel development.

Common branch examples:

  • main
  • develop
  • feature/homepage
  • bugfix/login

Creating a New Branch

Create a branch named feature/homepage:

git branch feature/homepage

Create AND switch to it immediately:

git checkout -b feature/homepage

Equivalent commands:

git branch feature/homepage
git checkout feature/homepage

Listing Branches

Show all local branches:

git branch

Show local and remote branches:

git branch -a

Example output:

* main
  feature/homepage

Switching Between Branches

Switch to another branch:

git checkout feature/homepage

Switch back to main:

git checkout main

Modern Git commands:

git switch feature/homepage
git switch main

Merging Branches

First switch to the branch you want to merge INTO:

git checkout main

Then merge your feature branch:

git merge feature/homepage

Git will attempt an automatic merge (fast-forward or merge commit).


Handling Merge Conflicts

If two branches edit the same lines of code, Git will produce a conflict.

A conflict looks like this:

<<<<<<< HEAD
your current changes
======
incoming changes from merged branch
>>>>>>> feature/homepage

Resolve conflicts by:

  1. Editing the file
  2. Choosing the correct content
  3. Removing conflict markers
  4. Staging the file:
git add filename
  1. Completing the merge:
git commit