Stage 2 · Tools
Remote Collaboration
Remotes
remote add, fetch, pull, push — how Git tracks remote branches.
What is a Remote?
A remote is a named reference to another repository, usually hosted on a server like GitHub, GitLab, or Bitbucket. The default remote is called origin, which points to the repository you cloned from. Every clone can have multiple remotes, and each remote can be fetched from or pushed to independently.
When you clone a repository, Git automatically creates a remote called origin and sets it as the default push target. It also creates remote-tracking branches like origin/main that mirror the state of the remote repository at the time of your last fetch.
git remote -vShows all configured remotes with their fetch and push URLs. The -v (verbose) flag reveals the actual URLs behind each remote name.
The name origin is a convention, not a requirement. You can name remotes anything — production, staging, upstream. What matters is the URL mapping, not the name.
Adding Remotes
Use git remote add to register a new remote. You can add as many remotes as you need, each pointing to a different repository or a different URL for the same repository.
git remote add upstream https://github.com/original-owner/repo.gitRegisters a new remote called upstream pointing to the given URL. You can now fetch from or push to this remote by name.
git remote add deploy git@github.com:myorg/deploy-config.gitSSH remotes use the git@ syntax. SSH keys must be configured on the server for authentication. No password prompts on each operation.
git remote set-url origin https://github.com/new-owner/repo.gitUpdates the URL for an existing remote. Useful when a repository is transferred to a different owner or when switching between HTTPS and SSH.
SSH authentication avoids entering credentials on every push/pull. Set up SSH keys once and Git operations become seamless.
fetch vs pull
git fetch downloads all new data from a remote without modifying your working tree. It updates remote-tracking branches (origin/main, origin/feature-x) so you can inspect what changed before merging. git pull is shorthand for git fetch followed by git merge (or git rebase with --rebase).
git fetch --allDownloads new commits, branches, and tags from every configured remote. Your working tree and local branches are untouched.
git fetch upstreamFetches only from the upstream remote. The remote-tracking branch upstream/main is updated to match the latest state of that remote.
git pull origin mainFetches from origin/main and immediately merges it into your current branch. Equivalent to git fetch origin && git merge origin/main.
git pull --rebase origin mainFetches from origin/main and rebases your local commits on top of the updated remote branch. This produces a linear history without merge commits.
Running git pull on a branch that others also work on can create unnecessary merge commits. Prefer git fetch + review + git rebase or git merge for controlled integration.
Pushing Changes
git push uploads your local commits to a remote. By default, it pushes to origin and the current branch's tracking branch. You must have write access to the remote, and the push will be rejected if it would overwrite commits you haven't pulled.
git push origin mainUploads all commits on your local main that are not yet on origin/main. The remote-tracking branch is updated after a successful push.
git push -u origin feature/authPushes the feature/auth branch to origin and sets it as the upstream tracking branch. After this, a plain git push from that branch will always push to origin/feature/auth.
git push --force-with-lease origin feature/authOverwrites the remote branch with your local version. --force-with-lease is safer than --force because it checks that you're not overwriting someone else's push.
Force pushing rewrites history. On main, develop, or any shared branch, this will break every teammate's repository. Only force push on personal feature branches.
Tracking Branches
A tracking branch is a local branch that is linked to a remote branch. When you clone, Git automatically sets main to track origin/main. Tracking means git pull and git push know which remote and branch to use without explicit arguments.
git branch -vvLists all local branches with their upstream tracking information. The * marks your current branch. You'll see something like * main [origin/main: ahead 2] indicating two local commits not yet pushed.
git branch -u origin/feature/apiSets the current branch to track origin/feature/api. After this, git push and git pull will use origin/feature/api as the default.
| Command | What it does | When to use |
|---|---|---|
| git push -u origin feature | Push + set upstream | First time pushing a new branch |
| git branch -u origin/main | Set upstream only | Existing branch needs tracking |
| git pull origin feature | Explicit pull | One-off pull from specific remote/branch |
origin/main is the remote-tracking branch — a snapshot of what main looked like on the server at your last fetch. main is your local branch. They diverge when you make local commits or when new commits are pushed to the server. Always git fetch before comparing.
Managing Remotes
Over time, your remote configuration may need maintenance — removing stale remotes, renaming them, or updating URLs. Git provides simple commands for all of these.
git remote remove upstreamDeletes the upstream remote and all of its tracking branches. The local commits and branches are not affected — only the remote reference is removed.
git remote rename origin ghRenames the origin remote to gh. All tracking branches are updated automatically: origin/main becomes gh/main.
git fetch --prune originRemoves remote-tracking branches for branches that no longer exist on the remote. Without pruning, you'll see stale references like origin/feature/old that are already deleted on the server.
Run git config fetch.prune true to make pruning automatic on every git fetch. This keeps your remote-tracking branches clean without thinking about it.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.