Skip to main content

Branching and Merging

In a professional environment like CodeHarborHub, we never work directly on the "Live" code (the main branch). Instead, we use Branches.

A branch is essentially a Parallel Universe. You can build a new feature, experiment with a new design, or fix a bug in a separate space without affecting the main project.

The Branching Workflowโ€‹

Imagine you are building a website. The main branch is the version users see. You want to add a "Dark Mode."

  1. Create a Branch: You step into a parallel universe called feat-dark-mode.
  2. Work: You write your CSS and JS. The main branch remains untouched and stable.
  3. Merge: Once the dark mode is perfect, you bring those changes back into the main universe.

Step 1: Creating and Switchingโ€‹

You can create a branch and move into it using these commands:

# Create the branch
git branch feat-dark-mode

# Switch to the branch
git checkout feat-dark-mode

Step 2: Merging Changesโ€‹

Once your work in the branch is finished and committed, itโ€™s time to merge it back to the main branch. Hereโ€™s how you do it:

1. Switch back to the destination (Main):

git checkout main

2. Pull the changes in:

git merge feat-dark-mode

3. Cleanup (Optional): Since the feature is now part of main, you can delete the temporary branch:

git branch -d feat-dark-mode

The Logic of Mergingโ€‹

Handling Merge Conflictsโ€‹

Sometimes, Git gets confused. If you changed line 10 in main and your friend changed line 10 in feat-dark-mode, Git won't know which one to keep. This is a Merge Conflict.

How to fix it:

  1. Open the file in VS Code.
  2. You will see markers: <<<<<<< HEAD (Your version) and >>>>>>> branch-name (Their version).
  3. Delete the version you don't want and remove the markers.
  4. git add the file and git commit to finish the merge.

Industrial Best Practicesโ€‹

RuleWhy?
Descriptive NamesUse feat/login-ui or fix/header-logo so others know what the branch is for.
Keep Branches SmallDon't build five features in one branch. One branch = One task.
Pull Before MergeAlways run git pull origin main before merging to ensure you have the latest team updates.
info

Not sure what branch you are on? Run git branch. The one with the asterisk (*) and highlighted in green is your current "Parallel Universe."