Skip to main content
Back to Blog

Git Branching, Merging, and Merge Conflicts: A Practical Beginner's Guide

Updated on November 20, 20268 minutes read


Why Branches Exist

Imagine you are building a login feature. It will take a few days, and the code will be broken for most of that time. Meanwhile, a teammate needs to fix a small bug on the same project and ship it today.

If you both edit the project directly, you will trip over each other. Branches solve this by giving each task its own safe space. You can experiment freely, and the stable version of the project stays untouched until your work is ready.

This guide assumes you know the basics of staging and committing. Here, we put branches to work: creating them, merging them, and handling the conflicts that sometimes appear.

What Is a Git Branch?

A branch is a movable label that points to a commit. Because Git stores your project as a chain of commits, a branch simply marks where one line of work currently ends. Creating a branch is fast and cheap, since Git is only creating a new pointer.

Git also tracks HEAD, a pointer that tells Git which branch you are currently on. When you make a commit, it is added to whichever branch HEAD points to. Knowing where HEAD is answers the most common beginner question: "Where will my commit go?"

Most repositories have a default branch, usually named main, though older projects may use master. Run git branch to see what yours is called.

Creating a Branch

Start from an up-to-date main, then create your feature branch. Branch names such as feature/login describe the work and keep things organized:

git switch main
git pull
git branch feature/login

The git branch <name> command creates the branch but does not move you onto it. Many developers use a shortcut that creates and switches in one step:

git switch -c feature/login

The -c flag means "create." Both git switch and its -c option require Git 2.23 or newer.

Switching Branches

Switching moves HEAD to another branch and updates your files to match it. Use git switch followed by the branch name:

git switch feature/login
git switch main

Before switching, it is best to commit your current work. If you have uncommitted changes that conflict with the target branch, Git will stop you rather than lose anything. Run git branch at any time to see all branches, with an asterisk marking the one you are on.

Making Commits

Once you are on feature/login, work as usual: edit files, stage them, and commit. These commits belong only to your branch, so main stays exactly as it was.

git add login.html
git commit -m "Add login form markup"

git add login.js
git commit -m "Add login form validation"

To see how your branches relate, use a compact graph view:

git log --oneline --graph --all

You will see feature/login running ahead of main, with each commit listed. This view becomes very useful once branches start to diverge.

Merging a Branch

Merging combines the history of one branch into another. To bring your finished feature into main, switch to main first, then merge the feature branch into it:

git switch main
git pull
git merge feature/login

The direction matters: you merge into the branch you are currently on. If main has not changed since you branched off, Git simply moves the main pointer forward, which is called a fast-forward merge. If main has new commits of its own, Git creates a merge commit that joins the two histories.

What Causes Merge Conflicts?

Most merges succeed automatically, even when both branches changed things. Git can combine edits to different files, or to different parts of the same file, without any help.

A merge conflict happens when both branches changed the same lines, or when one branch edited a file that the other deleted. Git cannot know which change is correct, so it pauses and asks you to decide. This is not an error in your code. It is Git asking for a human decision.

How to Read a Merge Conflict

Suppose a teammate changed the heading in header.html on main, while you changed the same line on feature/login. When you merge, Git reports a conflict, and git status lists the file under "Unmerged paths" as "both modified."

Open the file, and you will see conflict markers:

<<<<<<< HEAD
<h1>Welcome to Our Store</h1>
=======
<h1>Sign In</h1>
>>>>>>> feature/login

Illustration of a merge conflict in a code editor showing the current branch version and the incoming branch version.

Here is how to read it:

  • Everything between <<<<<<< HEAD and ======= is the version from the branch you are on (here, main).
  • Everything between ======= and >>>>>>> feature/login is the version from the branch being merged in.

How to Resolve a Merge Conflict

Resolving a conflict means editing the file into the final version you want, then telling Git you are done. Follow these steps.

1. Edit the file. Choose one version, or combine them, and delete all three marker lines. For example:

<h1>Sign In to Our Store</h1>

2. Stage the resolved file. This tells Git the conflict in that file is handled:

git add header.html

3. Complete the merge. Run git commit, and Git opens a pre-filled merge message you can accept:

git commit

If you decide the merge is a mistake, you can cancel and return to where you started with git merge --abort. Always search your files for leftover <<<<<<< markers before committing, since they are easy to miss.

Complete Branching and Merging Workflow

Here is the full workflow from start to finish, following the path main → feature/login → commits → merge back to main.

Diagram of the Git workflow: main branch, feature/login branch with commits, and a merge back into main.

StepCommandPurpose
1git switch main and git pullStart from the latest main
2git switch -c feature/loginCreate and enter your branch
3git add and git commitSave work on your branch, repeating as needed
4git switch main and git pullReturn to main and fetch teammates' updates
5git merge feature/loginCombine your work into main
6Resolve conflicts, if anyEdit, git add, then git commit
7git pushShare the merged result
8git branch -d feature/loginDelete the finished branch

The -d option only deletes a branch that has already been merged, so it protects you from losing unmerged work. If a teammate's changes land on main while you work, a conflict like the one above is possible whenever you both touched the same lines, and now you know how to handle it.

Common Mistakes

These errors catch almost every beginner, so they are worth knowing in advance.

Committing on the wrong branch. Always check git branch or git status before committing. It is easy to forget you are still on main.

Merging in the wrong direction. Remember that you merge into the branch you are on. Switch to the destination branch first.

Skipping git pull. Merging with an outdated main makes conflicts more likely and harder to understand.

Leaving conflict markers in the file. If <<<<<<< or >>>>>>> ends up in your committed code, it will break the file. Search for them before you commit.

Letting branches live too long. The longer a branch stays separate, the more it drifts from main, and the larger the conflicts become.

Panicking during a conflict. A conflict is a normal, fixable situation. If you feel lost, git merge --abort returns you to a clean starting point.

Branching Best Practices for Beginners

A few habits keep your branches simple and your merges smooth.

  • Create one branch per task, so each branch has a single clear purpose.
  • Use descriptive names, such as feature/login or fix/header-typo.
  • Commit small and often, with clear messages that explain what changed.
  • Pull the latest main before you start and before you merge.
  • Keep branches short-lived, and merge them as soon as the work is ready.
  • Delete branches after merging to keep your repository tidy.

Practical Cheat Sheet

TaskCommand
List branchesgit branch
Create a branchgit branch <name>
Create and switchgit switch -c <name>
Switch branchesgit switch <name>
See history as a graphgit log --oneline --graph --all
Merge a branch into the current onegit merge <name>
Cancel a merge in progressgit merge --abort
Delete a merged branchgit branch -d <name>

Conclusion and Next Steps

Branches let you work on new ideas without risking stable code. Merging brings that work back together, and conflicts are simply the moments when Git needs your judgment. Once you have resolved a few, they stop feeling intimidating.

Branching and merging become easier through repeated practice, especially across the situations that tend to cause trouble, such as simultaneous edits and long-lived branches. If you want to build a broader Git and version-control foundation, one structured way to do it is through Code Labs Academy's self-paced Git Fundamentals for Beginners course, which offers a guided learning path for these skills.

Whichever route you take, try this workflow on a small practice repository. Create a branch, cause a conflict on purpose, and resolve it. That one exercise teaches more than reading alone.

Learn technical skills online with Code Labs Academy

Learn technical skills online with Code Labs Academy

Join our supportive community, unlock your potential, and keep growing in your career.

Apply Now

Frequently asked questions

What is a Git branch?

A Git branch is a movable pointer to a commit that represents an independent line of development. It lets you work on a feature or fix without changing the stable version of your project.

What is HEAD in Git?

HEAD is a pointer that shows which branch, and therefore which commit, you are currently working on. New commits are added to the branch that HEAD points to.

What causes a merge conflict?

A merge conflict happens when two branches change the same lines of a file, or when one branch edits a file that another deletes. Git cannot decide which change to keep, so it asks you to resolve it manually.

How do I resolve a merge conflict in Git?

Open the conflicted file, edit it into the version you want, and remove the conflict markers. Then stage the file with git add and finish the merge with git commit.

Can I cancel a merge if something goes wrong?

Yes. Running git merge --abort while a merge is in progress cancels it and returns your repository to the state it was in before you started the merge.

Career services

Personalized support to help you advance your career. Get résumé reviews, mock interviews, and industry insights, so you can showcase your new skills with confidence.

Explore