Git Branching

Welcome to this in-depth look at Git branches. Think of Git branching like creating multiple “work areas” in your project, each dedicated to a particular feature or fix. This helps you keep your code organized and safe from accidental interference with the main application flow. By the end of this tutorial, you’ll understand the basics of branching, how to switch between branches, and how to merge your work back into the main part of your project.

Branch Basics and Benefits

A branch in Git is basically an independent line of development. Imagine you’re writing a novel. You have a main storyline (the main branch), but you also want to explore alternative plots or new chapters without risking the continuity of your main story. That’s what a branch lets you do.

Here are the primary benefits of branching:

Here’s a simplified diagram to show what it might look like:

Example Git branches diagram

Notice there’s a main branch (production-ready code) and a dev branch (staging ground for new features). Each new feature is branched off dev, worked on, then merged back into dev. Later, some features get merged into main for release. This setup allows for controlled deployments and flexible development timelines.

Working on Branches – git branch and git checkout

Git offers straightforward commands to handle branches. First, you can list all existing branches with:

git branch

This shows something like:

  main
  dev
* first-feature-name
  second-feature-name
  third-feature-name

The asterisk indicates the active branch (first-feature-name in this example).

To switch (“check out”) another branch:

git checkout <branch-name>

This moves your working directory to that branch’s commit history. You can see how each branch includes certain commits at the time it was created, plus any new commits added to it thereafter. Branches are like parallel timelines—some commits appear only on one branch until you merge them.

To create a new branch and switch to it immediately, you can use:

git checkout -b <new-branch-name>

Tip: Always create new feature branches from your main development branch (often dev), so the new branch includes all the latest changes from dev.

Merging Branches – git merge

Once you finish a feature on your branch, you’ll want to combine (merge) that branch’s commits into another branch. In most setups, you merge feature branches into your main development branch (dev), and later from dev to main for production release.

The syntax is:

git checkout <branch-to-merge-into>
git merge <other-branch>

For example, to merge feature-abc into dev:

git checkout dev
git merge feature-abc

If you have any conflicts (i.e., the same lines changed differently in both branches), Git will prompt you to resolve them, then you’ll create a new commit to finalize the merge. This is part of the normal development process; it’s like reconciling different story edits to ensure everything aligns properly.

Sample Feature Workflow

Let’s walk through an example in an AirBnB-style project:

Suppose user-auth is merged into dev, and later into main, meaning it’s now deployed. Meanwhile, get-all-spots might still be merged only to dev, not yet released to production.

You want to implement a new feature: get-spot-by-id. Here’s the step-by-step:

  1. Check out dev:
    git checkout dev
    
    Now dev is active.
  2. Create and switch to a new branch:
    git checkout -b get-spot-by-id
    
    This new branch is based on dev, ensuring it has the latest changes.
  3. Develop the feature: Make code changes, run git add and git commit repeatedly as you progress.
  4. Merge back into dev:
    git checkout dev
    git merge get-spot-by-id
    
    This brings all commits from get-spot-by-id into dev.
  5. Push to the remote repository:
    git push origin dev
    
    Now your dev branch on GitHub matches what you have locally.
  6. Cleanup (optional): Once everything’s tested and merged, you can delete the get-spot-by-id branch if you like:
    git branch -d get-spot-by-id
    
    (Or git push origin --delete get-spot-by-id to remove it from remote as well.)

Finally, when you’re ready to deploy, you merge dev into main. This separates your ongoing development from the production environment, allowing you to decide exactly which features go live and when.

What You’ve Learned

Git branching is a powerful way to keep code changes organized, prevent conflicts, and maintain multiple lines of development simultaneously. Key points to remember:

By mastering branching, you can take your development workflow to a new level of organization and collaboration, ensuring each feature is developed cleanly and confidently before joining the main codebase.