🌳 GitHub Branching: A Guide for Two

Learn how to work together on the same project without stepping on each other's toes

What is a Branch?

A branch is like a parallel copy of your entire project where you can make changes without touching the main code. Think of it like creating a backup of your game before you try a big new featureβ€”if it breaks, your original is still safe.

Core Concepts

Main Branch

The "official" version of your project. In GitHub, this is usually called main or master. You keep it clean and working.

Feature Branch

A branch you create to work on one thingβ€”a new feature, a bug fix, or a new game level. You name it something descriptive like add-multiplayer or fix-collision-bug.

Commit

A snapshot of your code at one moment in time. Every commit has a message describing what changed. Think of it like saving your game.

Pull Request (PR)

Your way of saying "Hey, I made some changes on my branchβ€”can you review them and merge them into main?" It's like asking "Does this look good?"

Merge

Combining two branches. When your changes are approved, you merge your feature branch back into main. Both people's work comes together smoothly.

Conflict

When two people changed the same line of code in different ways, Git doesn't know which version to keep. Don't panicβ€”GitHub helps you fix it.

Getting Started: Step by Step

1One of You: Set Up the Repo (Owner)

If you already have a GitHub repo, skip to step 3.

The project owner: Create a new repo on GitHub and add a README.md file. Then invite your collaborator to the repo (Settings β†’ Collaborators).

2Both of You: Clone the Repo

Clone means "download a copy of the entire project and all its history."

git clone https://github.com/yourusername/your-repo.git cd your-repo

Now you both have the same code on your computers, and Git is tracking it.

3You (Person A): Create a Feature Branch

You want to add a new feature. First, make sure you're on main, then create a new branch.

git checkout main git pull origin main git checkout -b add-score-system

Now you're on the add-score-system branch. Your changes won't affect anyone else.

βœ“ Naming tip: Branch names should be short and clear: add-lives-mechanic, fix-audio-bug, improve-ui-buttons. Use hyphens between words, no spaces.

4You (Person A): Make Changes & Commit

Edit your files, save them, and then tell Git to save them as a commit.

git add . git commit -m "Add score system with multiplier logic"

You can make multiple commits as you work. Each one is a checkpoint.

5You (Person A): Push Your Branch

Push sends your branch to GitHub so your partner can see it.

git push origin add-score-system

Your branch is now on GitHub. Your partner's code is untouched.

6Your Partner (Person B): Works on Their Own Branch

While you're working, your partner does the exact same thing on their own branch.

git checkout main git pull origin main git checkout -b add-sound-effects # ... make changes ... git add . git commit -m "Add victory and collision sounds" git push origin add-sound-effects

Now you have two feature branches on GitHub, and you haven't interfered with each other at all.

βœ“ The golden rule: Never commit directly to main. Always create a feature branch first. This keeps main clean and lets you review each other's work.

Merging Your Changes Back

1Create a Pull Request (PR) on GitHub

After you push your branch, go to your GitHub repo. You'll see a prompt to "Compare & pull request." Click it.

Write a good PR description:

## What does this do? Added a scoring system that tracks player points and applies a 2x multiplier when the player gets 5 consecutive hits. ## How do I test it? 1. Click on a target 5 times without missing 2. Check that the score displays the multiplier correctly 3. Verify the score resets when you miss ## Checklist - [x] Tested the feature myself - [x] No console errors - [x] Didn't break any existing code

2Your Partner Reviews the PR

They look at your changes, test them, and either approve or ask for fixes.

Things to look for:

  • Does the code make sense?
  • Did they break anything?
  • Are there any typos or obvious bugs?
  • Is it organized and readable?

If something needs fixing, they leave a comment, and you push a new commit to the same branch to fix it.

3Merge the PR

Once approved, click "Merge pull request" on GitHub. Your feature branch is now combined with main.

git checkout main git pull origin main

Now both of you have the new feature on your computers.

Handling Merge Conflicts

If both people changed the same line, GitHub will say there's a conflict. Don't panicβ€”it's easy to fix.

⚠ How conflicts happen: You and your partner both edit the same function in different ways on different branches. When you try to merge, Git doesn't know which version to keep.

GitHub will show you the conflict in the PR. Look for lines like this:

<<<<<<< HEAD // Your version function calculateScore() { return points * 2; } ======= // Their version function calculateScore() { return points + 10; } >>>>>>> add-sound-effects

To fix it: Talk to your partner. Decide which code is better, or combine both ideas. Then delete the conflict markers and keep only the code you want.

function calculateScore() { return (points * 2) + 10; }

Commit this fix, and the conflict is resolved.

βœ“ How to avoid conflicts: Talk to each other about what you're working on. If you're both editing the same file, coordinate. Have one person finish and merge first, then the other person pulls the latest code.

Git Commands You'll Need

Checking Status & Branches

# See what branch you're on and what files changed git status # List all your local branches git branch # List all branches (local + remote on GitHub) git branch -a

Switching & Creating Branches

# Switch to an existing branch git checkout main git checkout add-score-system # Create a new branch and switch to it git checkout -b add-new-feature # Delete a branch (after it's merged) git branch -d add-new-feature

Making & Saving Changes

# See all changes you made git diff # Add files to be committed git add . # Add all files git add filename.js # Add one file # Save your changes as a commit git commit -m "Clear message about what changed" # See your commit history git log

Syncing with GitHub

# Push your branch to GitHub git push origin add-score-system # Fetch the latest code from GitHub git fetch origin # Pull (fetch + merge) the latest main git pull origin main # See remote branches git remote -v

Undoing Mistakes

# Undo changes in a file (before committing) git checkout -- filename.js # Undo your last commit (keep the changes) git reset --soft HEAD~1 # Undo your last commit (discard the changes) git reset --hard HEAD~1
⚠ Before using git reset --hard: This deletes your changes permanently. Only use it if you're absolutely sure.

Branch Simulator

Click through a realistic workflow to see how branches work. This shows commits, branches, and merges as they happen.

Current State

main β†’ [v1.0]