Skip to Main Content






Git Basics

Branching and Merging

Branching and Merging

Branching and merging are key concepts in Git that allow multiple lines of development to exist independently and then be combined. This makes it easier to work on different features, experiments, or fixes simultaneously without disrupting the main codebase.

 

Branching and merging are powerful tools in Git that allow for flexible and safe development practices. By mastering these commands, you can manage complex development workflows, experiment with new ideas, and collaborate more effectively with your team.

Branching

Creating a Branch

  • Purpose: Branching allows you to create an independent line of development. You can use branches to work on new features, bug fixes, or other tasks without affecting the main branch (often called main or master).

  • Usage:

    git branch <branch-name>
     

  • Example:
    To create a new branch called landing-page, you would use:

    git branch landing-page
     

  • This command creates a new branch from the current commit, allowing you to start making changes that won’t affect other branches.

     

Switching Branches

  • Purpose: Switching branches allows you to move between different lines of development. When you switch to a branch, your working directory updates to reflect the state of that branch.

  • Usage:

    git checkout <branch-name>
     

  • Example:

    • To switch to the landing-page branch, you would use:

      git checkout landing-page

  • Your working directory will now reflect the state of the landing-page branch, and you can continue working on that branch.

  • Note: In newer versions of Git, you can create and switch to a new branch in one step using:

    git switch -c <branch-name>

Merging

Merging Branches

  • Purpose: Merging integrates changes from one branch into another. This is commonly done when you finish working on a feature branch and want to include those changes in the main branch.

  • Usage:

    git merge <branch-name>

  • Example:

To merge the landing-page branch into the main branch, first switch to main:

git checkout main

Then merge the changes from landing-page:

git merge landing-page

  • If there are no conflicts, Git will automatically combine the changes, and you’ll have a new commit representing the merge.

Resolving Merge Conflicts

  • Purpose:

A merge conflict occurs when Git cannot automatically resolve differences between branches. This usually happens when the same part of a file has been modified differently in the branches being merged.

  • Identifying Conflicts:

If a conflict occurs during a merge, Git will pause the merge process and mark the conflicted files with special conflict markers. The conflicted areas will look something like this:

<<<<<<< HEAD
content from the current branch
======= content from the branch being merged
>>>>>>> landing-page

 

Resolving Conflicts:

  • To resolve a conflict, you need to manually edit the file to reconcile the differences. You can choose to keep the changes from one branch, combine both changes, or rewrite the section altogether.

  • After editing the file to resolve the conflict, you need to stage the changes and complete the merge:

    git add <conflicted-file>
    git commit -m "Resolved merge conflict between main and landing-page"


     

    Example:

  • Suppose both main and landing-page modified the same line in login.js. During the merge, Git detects a conflict and pauses, indicating the conflict in login.js.
  • You open login.js, manually resolve the conflict by editing the lines, and then run:

    git add login.js git commit -m "Resolved conflict in login.js"
    The merge is now complete, and the conflict is resolved.