4. Branching and Merging

What is a branch?

In Git, a branch represents an independent line of development. Think of it as a way to request a fresh working directory, staging area, and project history. New commits are recorded in the history for the current branch, which results in a fork in the history of the project.

The default branch in Git is named master. As you make commits, you're given a pointer that moves forward with each new commit. When you create a branch, you're essentially creating a new pointer to the same commit you're on. As you make changes on this branch, the pointer will move forward, independent of other branches.

Branches are useful for:

  • Developing features

  • Fixing bugs

  • Experimenting without affecting the main codebase

Creating branches (git branch, git checkout)

git branch is used to manage branches:

  • To view all branches:

    
    git branch
    
  • To create a new branch:

    
    git branch new-branch-name
    

git checkout is used to switch between branches:

  • To switch to an existing branch:

    
    git checkout branch-name
    
  • To create a new branch and switch to it in one command:

    
    git checkout -b new-branch-name
    

In recent versions of Git, you can use git switch as a more intuitive way to change branches:

  • To switch to an existing branch:

    
    git switch branch-name
    
  • To create and switch to a new branch:

    
    git switch -c new-branch-name
    

Merging branches (git merge)

Merging is the process of integrating changes from one branch into another. It's how you combine the separate lines of development created by git branch back into a single branch.

  • To merge a branch into the current branch:

    
    git merge branch-name-to-merge
    

There are two types of merges:

  1. Fast-forward merge: If the current branch is directly behind the branch you're trying to merge, Git will move the current branch forward to match the merged branch. This type of merge keeps a linear commit history.

  2. Three-way merge: If the branches have diverged, Git will create a new commit that has two parent commits, effectively joining the two branches. This type of merge introduces a new commit and is used when a fast-forward merge is not possible.

Dealing with merge conflicts

Sometimes, when you try to merge branches, changes conflict with each other, and Git can't determine which change should take precedence. This results in a merge conflict.

When a conflict occurs, Git will mark the problematic area in the file:


<<<<<<< HEAD
This is the version in your current branch.
=======
This is the version in the branch you're trying to merge.
>>>>>>> branch-name

To resolve the conflict:

  1. Open the file in a text editor.

  2. Decide which version to keep or manually merge the changes.

  3. Remove the conflict markers (<<<<<<<, =======, >>>>>>>).

  4. Save the file.

  5. Stage the file with git add filename.

  6. Commit the changes with git commit.

It's essential to test the merged code to ensure that the conflict resolution didn't introduce any bugs. Some tools and IDEs provide graphical interfaces to help resolve merge conflicts, making the process more intuitive.

Last updated