git Distributed version control system, created by Linus Torvalds

Remotes

A remote is just a named URL pointing at another copy of the repository — typically hosted on GitHub, GitLab, or a private server. git clone automatically creates a remote named origin pointing at the URL you cloned from.

Managing remotes

git remote -v                                 # list remotes and their URLs
git remote add origin git@github.com:you/repo.git
git remote rename origin upstream
git remote remove upstream
git remote set-url origin git@github.com:you/repo.git

fetch vs. pull

  • git fetch downloads new commits and updates remote-tracking branches (e.g. origin/main) — it never touches your working directory or local branches. Safe to run anytime.
  • git pull is git fetch immediately followed by git merge (or rebase, with --rebase) into your current branch. It changes your working directory.
git fetch origin
git fetch --all               # fetch every remote
git pull                      # fetch + merge current branch's upstream
git pull --rebase             # fetch + rebase instead of merge

Pushing

git push origin main                    # push local main to origin's main
git push -u origin main                 # also set main to track origin/main
git push                                # once tracking is set, just this works
git push origin --delete feature-x      # delete a remote branch
git push --force-with-lease             # safer force-push after rewriting history

Prefer --force-with-lease over plain --force: it refuses to overwrite the remote branch if someone else pushed to it since your last fetch, whereas --force overwrites unconditionally.

Tracking branches

A local branch can be linked to a remote branch so plain git pull/ git push know where to sync:

git branch -u origin/main          # set upstream for the current branch
git branch -vv                     # show each branch's tracked upstream + ahead/behind
git status                         # also reports ahead/behind vs. upstream

Forks and multiple remotes

Contributing to a project you don’t have push access to usually means forking it on the host, then adding both your fork and the original as remotes so you can pull in upstream changes:

git remote add origin git@github.com:you/repo.git
git remote add upstream git@github.com:original-owner/repo.git
git fetch upstream
git merge upstream/main       # or: git rebase upstream/main

Next: Undoing Changes covers safely walking back mistakes, both local and already-pushed.