tech

Chapter 3: Branches & Remote Repositories

Learn how Git branches work, how remote repositories connect local work to GitHub, and how teams collaborate across parallel timelines.

Chapter 3: Branches & Remote Repositories

Chapter 3: Branches & Remote Repositories

In Git, parallel development is cheap and fast. In this chapter, we will learn what branches actually are under the hood, how local branches sync with remote repositories like GitHub, and how to combine parallel timelines.


🌿 The Real-World Analogy: Choose-Your-Own-Adventure Books

Imagine reading a choose-your-own-adventure book.

                                    ┌──► Page 45 (Timeline A: Open chest)
                                   /
Page 10 ──► Page 23 ──► Page 34 ───
                                   \
                                    └──► Page 62 (Timeline B: Run away)

At Page 34, you have a choice:

  • Option A: Open the chest (Go to Page 45).
  • Option B: Run away from the monster (Go to Page 62).

In the real world, you might put a bookmark at Page 34, read Page 45, and see what happens. If you die, you can return to the bookmark at Page 34 and choose Option B instead.

In Git, Page 34 is a commit, and the two bookmarks are branches. A branch allows you to split your project into multiple parallel universes. You can write experimental code on one branch, and if it fails, simply throw the branch away; your original code remains untouched.


📂 What is a Branch, Really?

In other older version control systems, creating a branch meant physically copying all your project folders. For large projects, this took time and disk space.

In Git, a branch is just a tiny text file containing the 40-character commit hash of the commit it points to.

          [Commit A] ──► [Commit B] ──► [Commit C] ◄── branch: main (points to Commit C)

                                            └── HEAD (points to main)

When you create a new branch:

git branch feature-login

Git simply creates a new text file named feature-login inside .git/refs/heads/ and writes the hash of Commit C into it.

When you switch to that branch:

git switch feature-login

Git changes HEAD to point to feature-login. When you make a new commit (Commit D), only the feature-login pointer moves forward. The main branch pointer stays behind.

                                         ┌──► [Commit D] ◄── feature-login (HEAD)
                                        /
          [Commit A] ──► [Commit B] ──► [Commit C] ◄── main

🔄 Combining Timelines: Merging vs. Rebasing

When you finish working on a feature branch, you want to bring those changes back into your master timeline (main). There are two primary ways to do this:

1. Merge (Preserves History Structure)

Merging takes two branch pointers and combines their latest snapshots.

  • Fast-Forward Merge: If main has not received any new commits since you branched off, Git simply slides the main pointer forward to match your feature branch pointer. No new merge commit is created.
  • Three-Way Merge: If main has received new commits while you were working, Git finds the last common ancestor commit, combines the changes, and automatically creates a new “Merge Commit” joining the two branches.
Merge Commit:
[main]          A ── B ── C ──────► G (Merge Commit)
                           \       /
[feature-login]             D ── E 

2. Rebase (Rewrites History for a Linear Timeline)

Rebasing shifts the starting point of your feature branch to a new base commit.

Before Rebase:
[main]          A ── B ── C
                           \
[feature-login]             D ── E

After Rebase:
[main]          A ── B ── C
                           \
[feature-login]             D' ── E' (Replayed on top of C)
  • Pros: Creates a perfectly straight commit history without merge commits.
  • Cons: Rewrites history. Golden Rule of Rebasing: Never rebase branches that you have pushed to a public remote server where others are working. It will break their local histories.

🌐 Local vs. Remote Repositories

Up to this point, all your work has stayed on your machine. To share with others, you need a Remote Repository hosted on a cloud platform like GitHub.

┌─────────────────────────────────┐           ┌─────────────────────────────────┐
│     Your Computer (Local)       │           │        GitHub (Remote)          │
│                                 │           │                                 │
│  [Working Dir]                  │           │                                 │
│        │ git add                │           │                                 │
│  [Staging Area]                 │           │                                 │
│        │ git commit             │           │                                 │
│  [Local Repo Database (.git)]  ─┼── push ──►│   [Remote Repo Database (.git)] │
│                                ◄┼── pull ───│                                 │
└─────────────────────────────────┘           └─────────────────────────────────┘

The Key Remote Sync Commands:

  1. git remote add origin <url> Tells your local repository where the remote server lives. origin is just a shorthand alias for this URL.
  2. git push origin main Uploads your local commits from the main branch to the remote repository.
  3. git fetch origin Downloads all commit metadata and branches from the remote, but does not touch your files. It allows you to see what your teammates have done without modifying your active code workspace.
  4. git pull origin main Combines git fetch and git merge in one command. It downloads remote changes and instantly attempts to merge them into your local workspace.

💡 Summary of Chapter 3

  • Branches are cheap pointers to commits, enabling parallel development timelines.
  • Merging joins timelines together, sometimes creating a merge commit.
  • Rebasing replays commits on top of a new base to keep history linear.
  • Remote commands (push, fetch, pull) sync files and histories between your local machine and remote servers like GitHub.

In the next chapter, we will address the single most intimidating part of Git: Merge Conflicts, and learn a simple, structured process to solve them.


👉 Go to Chapter 4: Merge Conflicts & Resolution

Related