Understanding Git and Version Control

Branching and Merging in Git

Branching is one of Git's most powerful features. It allows you to diverge from the main line of development and continue to do work without messing with that main line. This is incredibly useful for developing new features, fixing bugs, or experimenting with ideas in isolation.

Conceptual diagram illustrating Git branches diverging and merging.

What is a Branch?

In Git, a branch is essentially a lightweight movable pointer to one of your commits. The default branch name in Git is typically main (or historically master). When you make commits, you are advancing this pointer. When you create a new branch, you are creating a new pointer to the current commit. As you commit on the new branch, it moves forward independently of other branches.

This ability to create separate lines of development is fundamental to many Modern DevOps Practices, allowing for parallel work streams and safer integration.

Basic Branching Commands

Terminal screenshot showing various git branch commands in action.

Merging

Once you've completed work on a branch (e.g., a new feature is finished and tested), you'll often want to integrate those changes back into your main development line (e.g., the main branch). This process is called merging.

To merge, you first switch to the branch you want to merge *into* (the receiving branch), and then run the git merge command with the name of the branch you want to merge *from* (the source branch).

# Switch to the main branch
git checkout main

# Merge the feature-branch into main
git merge feature-branch

Merge Conflicts

Sometimes, Git cannot automatically merge changes. This happens when changes on the two branches conflict – for example, if the same lines of a file were modified differently on both branches. This is a merge conflict.

When a merge conflict occurs, Git will stop the merge process and tell you which files have conflicts. You need to manually resolve these conflicts:

  1. Open the conflicted file(s) in your editor. Git marks the conflicted areas like this:
    <<<<<<< HEAD
    This is the content from the current branch (e.g., main).
    =======
    This is the content from the branch being merged (e.g., feature-branch).
    >>>>>>> feature-branch
  2. Edit the file to fix the conflict, choosing which parts to keep, or combining them as needed. Remove the conflict markers (<<<<<<<, =======, >>>>>>>).
  3. Once resolved, stage the modified file: git add <filename>.
  4. Commit the merge: git commit (Git usually provides a default merge commit message).

Effectively handling merge conflicts is a key skill in collaborative projects, much like understanding how different components interact is crucial in understanding microservices architecture.

💡 Tip: Use git status frequently during a merge to see the state of conflicted files.
Abstract representation of resolving a merge conflict in code.

Common Branching Workflows

While you can use branches however you like, some common workflows have emerged:

Understanding branching and merging is fundamental to leveraging Git's full potential for both solo and team projects.

Next: Working with Remote Repositories ➡️