Branching and Merging

Learn about branching strategies, creating new branches (`git branch`), switching between branches (`git checkout`), and merging branches (`git merge`).


Learn Git and GitHub: Branching and Merging

Understanding Branching and Merging

Branching and merging are fundamental concepts in Git that allow you to work on different features or bug fixes in isolation without affecting the main codebase. They enable parallel development and make collaboration much easier.

Branching

A branch in Git is essentially a pointer to a specific commit. When you create a new branch, you're creating a new pointer that points to the same commit as the branch you branched from. Think of it as creating a parallel timeline of development.

Merging

Merging is the process of combining the changes from one branch into another. This usually involves taking the changes made on the source branch and applying them to the target branch. Git tries to do this automatically, but sometimes conflicts arise that need to be resolved manually.

Branching Strategies

There are various branching strategies you can adopt depending on the size and complexity of your project. Some common strategies include:

  • Gitflow: A more complex strategy suitable for projects with scheduled releases. Involves branches like master, develop, feature/*, release/*, and hotfix/*.
  • GitHub Flow: A simpler strategy focused on continuously deploying to production. You typically branch from main (or master) for each feature and deploy directly.
  • GitLab Flow: Similar to GitHub Flow but with added rules and recommendations, often incorporating environment-specific branches.
  • Feature Branching: A simple strategy where each new feature or bug fix gets its own branch. This keeps the main branch stable and makes it easier to track changes.

The right strategy depends on your specific needs and development workflow. Start with a simple strategy like Feature Branching and adapt as needed.

Working with Branches in Git

Creating a New Branch (git branch)

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

git branch my-new-feature

This creates a branch named "my-new-feature" pointing to the same commit as your current branch. It *does not* switch you to the new branch. You are still on the original branch.

Switching Between Branches (git checkout)

To switch to a different branch, use the git checkout command followed by the name of the branch.

git checkout my-new-feature

Now you are working on the "my-new-feature" branch. Any commits you make will only affect this branch until it is merged into another branch.

You can combine creating and switching to a new branch with the git checkout -b command:

git checkout -b another-feature

This will create a branch named "another-feature" and switch to it immediately.

Merging Branches (git merge)

To merge one branch into another, you first need to switch to the target branch (the branch you want to merge into). Then use the git merge command followed by the name of the branch you want to merge from.

git checkout main  # Switch to the main branch
git merge my-new-feature # Merge changes from my-new-feature into main

This will attempt to merge the changes from "my-new-feature" into "main". If there are no conflicts, Git will automatically perform the merge. If there are conflicts, you will need to resolve them manually.

Resolving Merge Conflicts

When Git encounters conflicts during a merge, it will mark the conflicting sections in the affected files. You'll need to open these files, manually edit them to resolve the conflicts, and then stage and commit the changes.

The conflicting sections will typically look something like this:

<<<<<<< HEAD
// Changes from the current branch (main)
console.log("Hello from main");
========
// Changes from the branch being merged (my-new-feature)
console.log("Hello from the feature");
>>>>>>> my-new-feature 

You need to decide which changes to keep (or combine them), remove the conflict markers (<<<<<<< HEAD, ========, >>>>>>> my-new-feature), save the file, stage the changes (git add .), and then commit the merge (git commit).