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

Here is how to read it:
- Everything between
<<<<<<< HEADand=======is the version from the branch you are on (here,main). - Everything between
=======and>>>>>>> feature/loginis 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.

| Step | Command | Purpose |
|---|---|---|
| 1 | git switch main and git pull | Start from the latest main |
| 2 | git switch -c feature/login | Create and enter your branch |
| 3 | git add and git commit | Save work on your branch, repeating as needed |
| 4 | git switch main and git pull | Return to main and fetch teammates' updates |
| 5 | git merge feature/login | Combine your work into main |
| 6 | Resolve conflicts, if any | Edit, git add, then git commit |
| 7 | git push | Share the merged result |
| 8 | git branch -d feature/login | Delete 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/loginorfix/header-typo. - Commit small and often, with clear messages that explain what changed.
- Pull the latest
mainbefore 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
| Task | Command |
|---|---|
| List branches | git branch |
| Create a branch | git branch <name> |
| Create and switch | git switch -c <name> |
| Switch branches | git switch <name> |
| See history as a graph | git log --oneline --graph --all |
| Merge a branch into the current one | git merge <name> |
| Cancel a merge in progress | git merge --abort |
| Delete a merged branch | git 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
Join our supportive community, unlock your potential, and keep growing in your career.
Apply Now