Git
Intermediate 13 min read Git

Remotes & Collaboration

A remote is another copy of the repository, usually on GitHub or GitLab. Local commits exist before push; clone, fetch, pull and push move commits between your repo and the remote. Pull requests turn branch merges into reviewed, gated changes.

remoteoriginclonefetchpushpullpull request
Introduction

What is this?

A remote is a pointer to another repository — typically a server like GitHub, GitLab or Bitbucket. Because Git is distributed, commits you make locally do not exist anywhere else until you push them. git pull is literally git fetch followed by a merge of the remote branch into yours.

Motivation

Why do you need it?

  • A shared remote is the collaboration point for a team and a full backup of history.
  • Developers commit and branch locally and sync on their own schedule.
  • Pull requests give every merge a review and CI gate.
  • Remote-tracking branches (origin/main) let you see how far ahead or behind you are.
Think about it

Mental model

Imagine

A shared library

Think of

A git remote

Everyone can borrow the same book (clone), read the new chapters (fetch), and return notes (push). The library is the source of truth everyone syncs against.

Mechanics

How it works

  • git clone <url> downloads the entire repository — all commits, branches and tags — and records the source as 'origin'.
  • Local branches can track remote branches, e.g. main tracks origin/main.
  • git fetch downloads new remote refs without touching your working tree.
  • git pull = git fetch + git merge of the tracked remote branch.
  • git push uploads your local commits to the remote, refusing if the remote has commits you lack (non-fast-forward).

Synchronizing with a remote repository.

Local commits
git push
origin (remote)
git fetch / pull
Collaborators
Reference

Key commands

git clone https://github.com/user/repo.git

Download a full copy of a remote repository.

git remote -v

List configured remotes and their URLs.

git remote add origin <url>

Register a new remote named origin.

git fetch origin

Download remote refs without merging.

git pull --rebase

Fetch and rebase your local commits onto the remote branch.

git push -u origin main

Push the main branch and set its upstream tracking.

Hands-on

Examples

Push your first branch to GitHub

Push your first branch to GitHub
git remote add origin https://github.com/you/project.git
git push -u origin main
# Branch 'main' set up to track 'origin/main'.
# Everything up-to-date / new commits pushed.
git pull
# Merge the latest changes from origin/main.

git remote add origin registers the GitHub URL; git push -u origin main uploads the branch and records the upstream so plain git push and git pull work afterward. git pull then merges remote changes into your local branch.

Real world

In production

  • Pull requests on GitHub/GitLab gate merges with review and required CI checks.
  • Protected branches on the remote prevent direct pushes to main.
  • Every push can trigger CI/CD and ephemeral preview environments.
  • Tags and releases pushed to the remote feed artifact registries and deployment pipelines.
Guidelines

Best practices

  • Clone over SSH or HTTPS with a credential helper rather than storing passwords on disk.
  • Pull (preferably pull --rebase) before you push so your local branch stays in sync.
  • Open a pull request instead of pushing directly to a protected branch.
  • Use git fetch --prune to remove stale remote-tracking branches.
  • Push tags explicitly with git push --tags when a release is cut.
Watch out

Common mistakes

  • Running git push --force and clobbering history others depend on.
  • Committing large binaries or secrets, then pushing them where they can never be fully retracted.
  • Skipping the pre-push pull and fighting 'rejected non-fast-forward' errors.
  • Forgetting to set an upstream, then wondering why git push says the branch has no remote.
Root cause analysis

Common issues

Error
! [rejected] main -> main (non-fast-forward)
Root cause

The remote has commits you do not have locally.

Fix

Run git pull --rebase to rebase your work, then git push again.

Error
fatal: Authentication failed for 'https://...'
Root cause

No valid credentials for the remote (password auth removed by GitHub).

Fix

Use a personal access token with git credential manager, or switch to SSH keys.

Error
fatal: unable to access '...': Could not resolve host
Root cause

Network/DNS problem reaching the remote host.

Fix

Check connectivity and proxy settings; verify the repository URL in git remote -v.

When things break

Debugging tips

  • git remote -v to confirm which URLs your remotes point at.
  • git status shows 'Your branch is ahead of/behind origin/main' by how many commits.
  • git log origin/main..HEAD lists commits you have not pushed yet.
  • git fetch --prune removes tracking refs for branches deleted on the remote.
Interviews

Interview checkpoint

Practice

Hands-on lab

Collaborate Through a Remote Repository
30 min · intermediate
In progress
Objectives
  • Create a bare remote repository
  • Clone and push to it
  • Pull changes made by another clone
  • Inspect remote-tracking branches

Collaborate Through a Remote Repository

30 min · intermediate

0/6 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/3
1

Which command downloads remote commits WITHOUT merging them into your branch?

2

git clone downloads the full history of a repository, not just the latest commit.

3

git pull is equivalent to which combination?

Quick reference

Cheat sheet

Remotes
git clone URLFull copy of a repo
git remote -vList remotes and URLs
git remote add origin URLAdd a remote
git fetchDownload refs only
git pull --rebaseFetch + rebase
git push -u origin mainPush and set upstream
Go deeper

Official documentation