codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
📦

Git & GitHub

9 / 63 topics
9Branching and Merging10Resolving Merge Conflicts11Using git stash12Using git rebase
Tutorials/Git & GitHub/Branching and Merging
📦Git & GitHub

Branching and Merging

Updated 2026-04-20
3 min read

Branching and Merging

Introduction

Branching and merging are fundamental concepts in version control systems like Git, enabling developers to work on multiple features or bug fixes simultaneously without interfering with each other's work. This tutorial will guide you through the process of branching and merging using Git and GitHub, providing real-world examples and best practices.

Understanding Branches

A branch in Git is a lightweight movable pointer to one of these commits. The default branch name in Git is main (formerly known as master). When you create a new branch, it points to the same commit as the current branch.

Creating a New Branch

To create a new branch, use the git branch command followed by the branch name:

# Create a new branch named 'feature-branch'
git branch feature-branch

This command creates a new branch but does not switch to it. To switch to the newly created branch, use the git checkout command:

# Switch to the 'feature-branch' branch
git checkout feature-branch

Alternatively, you can create and switch to a new branch in one step using the -b flag with git checkout:

# Create and switch to 'feature-branch'
git checkout -b feature-branch

Listing Branches

To list all branches in your repository, use the git branch command. The current branch will be highlighted with an asterisk (*):

# List all branches
git branch

Working on a Branch

Once you are on a new branch, you can start making changes to your codebase. These changes will only affect the current branch until they are merged into another branch.

Making Changes

Let's say you want to add a new feature to your project. You would create a new branch for this feature and make the necessary changes:

# Create and switch to 'new-feature' branch
git checkout -b new-feature

# Make changes to files, e.g., add a new file or modify existing ones
echo "New feature content" > new_feature.txt

# Stage the changes
git add new_feature.txt

# Commit the changes with a descriptive message
git commit -m "Add new feature"

Merging Branches

Merging is the process of combining the changes from one branch into another. This is typically done to integrate features or bug fixes back into the main development branch.

Basic Merge

To merge changes from new-feature into main, follow these steps:

  1. Switch to the target branch (main in this case):

    # Switch to 'main' branch
    git checkout main
    
  2. Pull the latest changes from the remote repository (if necessary):

    # Pull latest changes from 'origin/main'
    git pull origin main
    
  3. Merge the new-feature branch into main:

    # Merge 'new-feature' into 'main'
    git merge new-feature
    
  4. Resolve any conflicts (if any) and commit the merge.

Fast-Forward Merge

If there are no conflicting changes, Git will perform a fast-forward merge, which simply moves the pointer of the target branch to the tip of the source branch:

# Fast-forward merge 'new-feature' into 'main'
git merge new-feature

Three-Way Merge

When there are conflicts or non-linear histories, Git performs a three-way merge. This involves creating a new commit that combines changes from both branches.

  1. Identify conflicts during the merge process:

    # Attempt to merge 'new-feature' into 'main'
    git merge new-feature
    
  2. Resolve conflicts by editing the affected files. Git marks conflicts with special markers (<<<<<<<, =======, and >>>>>>>).

  3. After resolving conflicts, stage the changes:

    # Stage resolved files
    git add <resolved-file>
    
  4. Commit the merge:

    # Commit the merge
    git commit -m "Merge new-feature into main"
    

Best Practices

1. Use Descriptive Branch Names

Branch names should be descriptive and convey the purpose of the branch, such as feature-user-authentication or bugfix-login-issue.

2. Keep Branches Short-Lived

Create branches for specific tasks and aim to complete them quickly. This reduces the risk of conflicts and makes it easier to manage changes.

3. Regularly Sync with Main

Frequently pull the latest changes from the main branch into your feature branch to minimize merge conflicts:

# Pull latest changes from 'origin/main' into 'feature-branch'
git checkout feature-branch
git pull origin main

4. Use Pull Requests for Merging

On GitHub, use pull requests (PRs) to review and merge code changes. This provides an opportunity for peer reviews and ensures that the code meets quality standards before being merged into the main branch.

  1. Push your feature branch to the remote repository:

    # Push 'feature-branch' to 'origin'
    git push origin feature-branch
    
  2. Create a pull request from feature-branch to main on GitHub.

  3. Request reviews and address any feedback.

  4. Once approved, merge the PR.

5. Resolve Conflicts Promptly

Conflicts should be resolved as soon as they are detected to avoid complications in the codebase.

Conclusion

Branching and merging are essential skills for effective collaboration and version control using Git and GitHub. By understanding how to create, switch, and manage branches, you can streamline your development workflow and ensure that your project remains organized and maintainable. Always follow best practices to minimize conflicts and maximize productivity.


PreviousUsing git diffNext Resolving Merge Conflicts

Recommended Gear

Using git diffResolving Merge Conflicts