50 Mins

5. Remote Repositories & GitHub

Collaboration

Remote Repositories & GitHub

Your local repository exists only on your machine. A remote repository is a copy of your repository stored on a server (like GitHub). Pushing to a remote serves two purposes: backup (your code survives if your laptop dies) and collaboration (your team can access and contribute to the code).

Connecting to a Remote

After creating a repo on GitHub, connect your local repo. origin is the conventional name for your primary remote. You can name it anything, but origin is the universal convention.

Bash
git remote add origin git@github.com:yourusername/your-repo.git

Verify the remote was added correctly:

Bash
git remote -v

^️ Push — Sending Your Commits to GitHub

Push your main branch to origin for the first time. The -u flag sets origin main as the default upstream:

Bash
git push -u origin main

After the first push, you can just use:

Bash
git push

Push a feature branch to remote (so teammates can see it):

Bash
git push -u origin feature/user-authentication

v️ Pull — Getting Updates from GitHub

Clone a repository from GitHub to your local machine:

Bash
git clone git@github.com:yourusername/your-repo.git

Get the latest changes from the remote (teammates' commits). pull = fetch + merge in one step. It downloads the remote changes AND merges them into your current branch:

Bash
git pull

Alternatively, fetch without merging (inspect before applying). This lets you check what is on the remote before you merge:

Bash
git fetch origin
git log origin/main
git merge origin/main

The Daily Collaboration Workflow

In a team using Git, the daily workflow follows this pattern — every single day, before starting work:

  1. Make sure you're on main:
    Bash
    git checkout main
  2. Pull the latest changes your teammates pushed yesterday:
    Bash
    git pull
  3. Create your branch for today's work:
    Bash
    git checkout -b feature/todays-feature
  4. Do your work...
  5. Stage and commit frequently (every logical unit of work):
    Bash
    git add .
    git commit -m "feat: add password validation to signup form"
  6. Push your branch to GitHub:
    Bash
    git push -u origin feature/todays-feature
  7. Open a Pull Request on GitHub: This is where code review happens before merging to main.

v️ Pulls — What Actually Happens Under the Hood

git pull is shorthand for two commands run back-to-back:

Bash
git fetch origin   # Download new data from remote (doesn't touch your files)
git merge origin/main  # Merge the downloaded data into your current branch

Understanding the two-step nature helps you diagnose pull failures:

1Fetch only

Use when you want to inspect remote changes before merging. Safe — nothing in your workspace changes.

2Pull (fetch + merge)

Use in daily workflow when you trust the remote and just want the latest code in your branch.

3Pull with rebase

git pull --rebase replays your local commits on top of the remote changes instead of creating a merge commit — keeps history linear.

Pull before you push — always. If you push without pulling first and a teammate has committed since you last synced, GitHub will reject your push with a non-fast-forward error. Always pull, resolve any conflicts, then push.

Knowledge Check

Ready to test your understanding of 5. Remote Repositories & GitHub?

Back