Back to Git & Dev Tools
2026-05-036 min read

Don’t be scared of branching or merging (Git & Dev Tools)

Learn Don’t be scared of branching or merging (Git & Dev Tools) step by step with clear examples and exercises.

Title: Mastering Branching and Merging with Git & Dev Tools

Why This Matters

Branching and merging are fundamental Git features that empower developers to manage their code effectively, especially in collaborative environments. They allow for experimentation, isolation of changes, and smooth integration of contributions from multiple team members. Understanding branching and merging is essential for tackling real-world issues, acing interviews, and collaborating efficiently on projects.

Prerequisites

Before diving into branching and merging, it's important to have a basic understanding of Git commands such as git init, git add, git commit, and git status. Familiarity with the command line is beneficial but not required, as many popular IDEs provide Git integration. Additionally, you should be comfortable navigating your file system and editing files.

Recommended Resources:

Core Concept

Branches in Git

A branch represents a separate development line within a repository. Each branch has its own history of commits, allowing you to work on different features or bug fixes without affecting the main project. By default, Git creates a master branch, which is the primary branch where production code resides.

To create a new branch:

git checkout -b <branch-name>

You can switch between branches using the following command:

git checkout <branch-name>

Merging Branches in Git

Merging is the process of combining changes from one branch into another. To merge a feature branch (e.g., feature/new-feature) into the main branch (master), use the following command:

git checkout master
git merge feature/new-feature

If there are any conflicts between the branches, Git will pause and allow you to resolve them manually. Once resolved, you can continue the merge process using the git mergetool command or manually editing the conflicting files. After resolving all conflicts, commit the changes with a descriptive message:

git add .
git commit -m "Merged feature/new-feature into master"

Merge Strategies

Git offers several merge strategies to handle merges more effectively. For example, the recursive strategy allows you to specify custom merge drivers for specific files or directories. You can configure these strategies using the git config command:

git config --global merge.<file-pattern>.strategy recursive

Merge Conflicts and Resolution

Merge conflicts occur when Git cannot automatically combine changes from two branches because they affect the same lines of code. To resolve merge conflicts, you need to manually edit the conflicting files by choosing which version of the code to keep or merging the changes yourself. Once resolved, stage and commit the changes as usual.

Resolving Merge Conflicts Manually

  1. Identify the conflicting files:
git status
  1. Open the conflicting file in your preferred text editor:
nano <file-name>
  1. Choose which version of the code to keep or merge the changes manually, and save the file.
  2. Stage and commit the resolved changes:
git add .
git commit -m "Resolved merge conflict"

Branching Strategies

There are several branching strategies that you can adopt to manage your repository more effectively. Some popular ones include:

  • Git Flow: a branching strategy for managing releases and hotfixes.
  • Feature Toggle: a method of implementing features without merging them into the main branch until they're ready for production.
  • Trunk-Based Development (TBD): a practice where developers work on small, incremental changes in the main branch, reducing the need for extensive branching and merging.

Worked Example

Let's say we have a simple index.html file in our repository:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>My Website</title>
</head>
<body>
<h1>Welcome to my website!</h1>
</body>
</html>

We create a new branch (feature/new-header) and modify the `` tag:

git checkout -b feature/new-header
echo "<h1>Hello, World!</h1>" >> index.html
git add .
git commit -m "Added new header"

We then switch back to the master branch and merge our changes:

git checkout master
git merge feature/new-header

Since we modified the same line of code in both branches, Git will flag a conflict:

Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

We can resolve the conflict by editing index.html and choosing to keep our new header:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>My Website</title>
</head>
<body>
<h1>Hello, World!</h1> <!-- Our new header -->
<h1>Welcome to my website!</h1> <!-- The original header -->
</body>
</html>

Once resolved, stage and commit the changes:

git add .
git commit -m "Resolved merge conflict"

Common Mistakes

1. Not creating a new branch before making changes

When working on a feature or bug fix, always create a new branch to isolate your changes and avoid affecting the main project until you're ready to merge them in.

Example:

Without creating a new branch, modifying index.html directly in the master branch will affect the live site immediately.

echo "<h1>Hello, World!</h1>" >> index.html
git add .
git commit -m "Added new header"

2. Merging without resolving conflicts

If Git flags a conflict during a merge, do not ignore it or force the merge (git merge --ignore-conflicts). Instead, resolve the conflicts manually before committing the merged changes.

Example:

Forcing a merge without resolving conflicts will result in unresolved conflicts that need to be addressed later.

git checkout master
git merge feature/new-header --ignore-conflicts

3. Not using descriptive commit messages

Clear and concise commit messages help other developers understand the purpose of your changes and make it easier to track the project's history.

Example:

Using vague or unhelpful commit messages makes it difficult for others to understand the context of your changes.

git commit -m "Changes"

Practice Questions

  1. Create a new branch (feature/new-page) for adding a new page to our repository, modify index.html accordingly, and merge the changes into the master branch using the recursive merge strategy.
  2. You are working on a feature branch (feature/login-system) that introduces a login system to an existing project. Another developer also creates a feature branch (feature/user-profile) for adding user profiles. Both branches have conflicts when merged into the master branch. How would you resolve the conflicts using the git mergetool command?

FAQ

Q: What happens if I don't merge my feature branch into the main branch after completing my work?

A: If you do not merge your feature branch into the main branch, your changes will be isolated and won't be part of the main project. To ensure your changes are included, you should merge or rebase your feature branch into the main branch before closing it.

Q: What is the difference between merging and rebasing in Git?

A: Merging combines two branches by creating a new commit that references both parent commits. Rebasing, on the other hand, moves all the commits from one branch onto another, effectively "replaying" them on top of the target branch. Rebasing is often used to clean up a feature branch's history before merging it into the main branch.

Q: What is a rebase and when should I use it?

A: Rebasing moves all the commits from one branch onto another, effectively "replaying" them on top of the target branch. This can help clean up a feature branch's history before merging it into the main branch or resolving conflicts more easily. However, be careful when using git rebase as it can create confusing histories if not done correctly.

Q: What is a merge commit and why are they sometimes avoided?

A: A merge commit is a special commit that combines changes from two branches. While they're necessary for merging branches, some developers prefer to avoid them because they can make the project history more complex and harder to navigate. Tools like git rebase or squashing commits can help minimize the number of merge commits in your repository.

Don’t be scared of branching or merging (Git & Dev Tools) | Git & Dev Tools | XQA Learn