Skip to main content

Git Branching and Merging

Welcome to the Branching & Merging section of the CodeHarborHub Git Tutorials. Branching is one of Gitโ€™s most powerful features โ€” allowing developers to experiment, fix bugs, and work on features without affecting the main code.


What is a Branch?โ€‹

A branch in Git is like a separate timeline of your project. Each branch lets you work independently from others โ€” perfect for building new features or fixing bugs safely.

Think of branches as โ€œparallel universesโ€ for your code โ€” you can switch between them anytime without disturbing the main project.

Default Branchโ€‹

When you create a new Git repository, Git automatically creates a default branch called:

  • master (in older versions) or
  • main (in newer repositories)

You can rename it anytime using:

git branch -M main

Creating a New Branchโ€‹

Use this command to create a new branch:

git branch feature-login

This creates a branch called feature-login but doesnโ€™t switch to it yet. To both create and switch at once:

git checkout -b feature-login

Youโ€™re now working inside your new branch!

Switching Between Branchesโ€‹

To view all branches in your repository:

git branch

To switch to another branch:

git checkout main

In modern Git versions, you can also use:

git switch main

Merging Branchesโ€‹

When your work in a branch is complete, youโ€™ll want to merge it back into the main branch.

Steps:โ€‹

  1. Switch to the branch you want to merge into:

    git checkout main
  2. Merge the feature branch:

    git merge feature-login
  3. If everything is fine, Git will perform a fast-forward merge (simple merge).

Merge Conflictsโ€‹

Sometimes, two branches modify the same part of a file, causing a merge conflict. Git will stop and ask you to resolve it manually.

Example Conflict Marker:โ€‹

<<<<<<< HEAD
console.log("Hello from main branch");
=======
console.log("Hello from feature branch");
>>>>>>> feature-login

Steps to Fix:โ€‹

  1. Edit the file to keep the correct version.

  2. Mark conflict as resolved:

    git add <filename>
  3. Complete the merge:

    git commit

Merge conflicts are normal โ€” they simply mean collaboration needs coordination.

Fast-Forward vs Recursive Mergeโ€‹

Merge TypeDescription
Fast-Forward MergeHappens when no new commits exist on the target branch; Git just moves the branch pointer forward.
Recursive MergeUsed when both branches have new commits โ€” Git combines them together.

Deleting Branchesโ€‹

Once youโ€™ve merged a feature branch, you can safely delete it:

git branch -d feature-login

If you want to force-delete it (even if not merged yet):

git branch -D feature-login

Viewing Branch Historyโ€‹

Use this to visualize your branches and merges:

git log --oneline --graph --all

Example Output:

* 9e15b3a Merge branch 'feature-login'
|\
| * 9d0c5b1 Added login feature
* | 2f1e3f9 Updated README
|/
* 4a8c7f2 Initial commit

Best Practices for Branchingโ€‹

  • Create branches per feature โ€” keeps code clean and isolated.
  • Use meaningful names โ€” e.g., feature/signup-form or bugfix/navbar-alignment.
  • Regularly pull from main โ€” keeps your branch up to date.
  • Delete merged branches โ€” avoids clutter.
  • Protect main branch on GitHub to prevent accidental pushes.

Team Workflow Exampleโ€‹

Hereโ€™s how most teams collaborate:

# Developer A
git checkout -b feature-homepage
# works and commits changes

# Developer B
git checkout -b feature-navbar
# works and commits changes

# Both push branches
git push origin feature-homepage
git push origin feature-navbar

# After review, merge them into main via Pull Request

Useful Commands Summaryโ€‹

CommandDescription
git branchList all branches
git branch <name>Create a new branch
git checkout <branch>Switch to a branch
git switch <branch>Switch branch (modern syntax)
git merge <branch>Merge a branch into current
git branch -d <branch>Delete a branch
git log --graph --onelineVisualize branch history

Next Stepโ€‹

Now that youโ€™ve mastered branching and merging, move on to the next tutorial: ๐Ÿ‘‰ Working with Remote Repositories

Here, youโ€™ll learn how to push, pull, and collaborate using GitHub remotes.


Additional Resourcesโ€‹


๐Ÿ’™ This tutorial is part of the CodeHarborHub Git & GitHub learning path โ€” helping developers collaborate, experiment, and innovate with confidence.