Understanding Git Branch Management

Learn Git branching concepts, branch operations, and practical techniques for resolving merge conflicts.

🎯 Introduction

Git is one of the most widely used version control tools, and branching is one of its most powerful core features. Branch management helps with collaboration, feature testing, and version control. In this article, you will learn what branches are, how to create, switch, and merge them, and how to resolve conflicts to improve your workflow and code quality.


🌿 What are branches, and why use them?

Think of branches as parallel versions of your code. They let you develop features, fix bugs, or experiment independently without affecting the main development line, usually the master or main branch.

Why use branches? Suppose you are collaborating on a project: one teammate is developing a feature while another fixes a bug. Without branches, coordinating changes can force people to work sequentially and lead to conflicts and confusion. Branches give each person a separate line of development that can be integrated into the main branch when ready, improving efficiency and protecting shared code.

Benefits of branches:

  • Isolate features: give each feature its own branch so work stays separate.
  • Develop in parallel: teammates can work on different features simultaneously.
  • Simplify rollback and version management: switch branches to return to another version when problems arise.
  • Improve code quality: test changes on a branch before merging them into the main branch.
Branching is central to Git. Understanding it gives you a strong foundation for using Git effectively.

🌱 Creating and switching branches

1. List existing branches

git branch

This lists all local branches and marks the current branch with *.

2. Create a branch

git branch new-branch-name

This creates a branch without switching to it.

3. Switch branches

git checkout branch-name

This switches to the specified branch. Before switching, save your changes in a commit or stash; Git may otherwise refuse to switch if changes would be overwritten.

4. Create and switch to a branch in one step

git checkout -b new-branch-name

This creates the branch and switches to it.

Tips

  • Use git branch -a to list local and remote-tracking branches.
  • Before switching branches, check git status to avoid losing changes.

🌳 Merging branches

After finishing work on a branch, you usually merge its changes back into the main branch.

1. Switch to the target branch (such as master)

git checkout master

2. Merge the branch

git merge new-branch-name

This merges changes from the named branch into your current branch.

3. Delete the branch after merging

git branch -d new-branch-name

Once the branch has been merged, it can be safely deleted.

Merge strategies

  • Fast-forward merge: if the current branch has no new commits of its own, Git moves its pointer to the incoming branch's commit, keeping history linear.
  • Three-way merge: when both branches have their own commits, Git attempts to combine their changes automatically. Any conflicts require manual resolution.
Switch to the intended target branch before merging so the changes are merged into the correct branch.

🔧 Resolving conflicts in practice

Conflicts can occur when merging branches. A conflict arises when both branches change the same part of a file and Git cannot determine which version to keep.

1. Identify the conflict

After git merge, Git reports the conflicting files and pauses the merge.

2. Check the conflicting files

git status

Conflicting files are marked both modified.

3. Open a conflicting file to see content like this:

<<<<<<< HEAD
Code from the current branch
=======
Code from the incoming branch
>>>>>>> new-branch-name

Edit the file manually to keep one version or combine both versions.

4. Mark the conflict as resolved after editing

git add conflicting-filename

5. Commit the completed merge

git commit

Tips for resolving conflicts

  • Graphical tools such as VS Code and GitKraken can make conflict resolution easier to understand.
  • Avoid having multiple people edit the same file for long periods to reduce conflicts.
  • Synchronize branches frequently and merge regularly to prevent conflicts from accumulating.
Take your time with conflicts. Understand what each change does before deciding how to combine the code.

❓ Knowledge check

Q1: What is a Git branch?

A branch is an independent line of development that lets developers work in parallel without affecting each other's work.

Q2: How do I create and switch branches?

Use git branch branch-name to create a branch and git checkout branch-name to switch to it, or use git checkout -b branch-name to do both at once.

Q3: What should I do if a merge causes conflicts?

Open the conflicting files, find the <<<<<<< markers, edit and save the resolved content, mark it as resolved with git add, and commit the merge.


🧪 Exercises

Exercise 1: Create and switch to a branch

Create a branch named feature-login and switch to it.

git checkout -b feature-login

Exercise 2: Merge branches and resolve a conflict

  1. Edit index.html on feature-login and commit the change.
  2. Switch back to master, edit index.html, and commit. Change the same part of the file to create a conflict.
  3. Run git merge feature-login to trigger the conflict.
  4. Open index.html, resolve the conflict manually, and commit.
git checkout master
git merge feature-login
# If a conflict occurs, edit index.html manually
git add index.html
git commit

🎉 You now understand the basics of branch management and have practiced using branches! Apply these skills to collaborate more efficiently on your projects.