135.4 Branching and merging
Learn how branches help you work on features independently, and how to merge them safely into the main codebase.
What is a branch?
A branch is a separate line of development in a Git repository. It allows you to make changes without affecting the main
version of your project.
Branches are commonly used to:
Build new features
Fix bugs
Try out ideas
Work on different tasks without interfering with each other
Every Git repository starts with a default branch called main
.
You can create a new branch to work on something independently, then merge it back into main
when it’s ready.
Why use branches?
Using branches keeps your main codebase clean and stable.
For example:
You create a branch called
feature-login
You build and test the login feature
Once it’s working, you merge it into
main
If something goes wrong, the rest of your code isn’t affected.
This process also supports collaboration, as multiple team members can work on their own branches simultaneously.

In the Git graph above, Developer-1 is taking the branch from master to develop feature-1 (assuming File-1 and File-3 will be changed for development). Therefore, the developer can safely commit their changes to both files in the master branch.
Developer-2 is taking the branch from master to develop feature-2 (assuming File-2 and File-4 will be changed for development).
Branching in Git involves logically copying the codebase. When it is merged into the master branch, only the snapshot of the changes from the feature branch is appended to the master branch. In other version control systems, like SVN, branches are copied when cutting the branch and replaced when merging with the master branch.
Common Git branch commands
git branch # List all branches
git branch new-feature # Create a new branch
git checkout new-feature # Switch to that branch
git switch new-feature # (Alternative modern command)
git merge new-feature # Merge branch into current one
A typical branching timeline might look like this:
main: A──B─────E────F
\
feature: C────D
A and B are commits on
main
You branch off to
feature
, adding commits C and DWhen the feature is complete, you merge it back into
main
, creating commits E and F
Merging and merge conflicts
When you merge one branch into another, Git attempts to combine the changes automatically. If both branches edited the same part of a file, you get a merge conflict.
Git will mark the conflict in the file like this:
<<<<<<< HEAD
print("Hello from main")
=======
print("Hello from feature branch")
>>>>>>> feature
You must resolve the conflict manually, then add and commit the resolved file.
Example merge steps:
git checkout main
git merge feature-branch
# If there's a conflict:
# 1. Open the file and fix the conflict
# 2. git add resolved_file.py
# 3. git commit -m "Resolve merge conflict"
Deleting a branch
Once a feature has been merged, you can delete the branch:
git branch -d feature-branch
This keeps your workspace clean.
Summary
A branch lets you work on a task without affecting the main project.
You can merge branches once the work is complete.
Merge conflicts happen when the same part of a file is changed in two branches.
Branching and merging are essential for collaborative and safe software development.
In the next section, you’ll learn how to apply version control within a larger workflow, using Git to support Agile project development.
Last updated
Was this helpful?