Git Remotes and Tracking Branches

Understand remotes, remote-tracking branches and upstreams, and how fetch, pull and push keep your repository in sync.

Lesson 11 of 30Intermediate17 min read

So far, everything you've done with Git has happened inside one repository on your computer. Real projects live in several places at once: your laptop, a teammate's computer, and a shared copy on a server such as GitHub. Each of those other copies is called a remote.

Git keeps track of what each remote looked like the last time you talked to it, using special remote-tracking branches such as origin/main. Three commands move work between your repository and a remote: git fetch downloads new commits, git pull downloads and combines them with your work, and git push uploads your commits.

Understanding how these fit together removes most of the confusion people feel about "ahead", "behind", "diverged" and "rejected". This lesson focuses on Git's side of the story; the GitHub push and pull lesson covers creating the repository on GitHub and signing in.

Remotes

A remote is simply a name for another repository's address:

Terminal
git remote -v
git remote add origin https://github.com/samlee/portfolio.git
git remote rename origin github
git remote remove old-server
  • origin is the conventional name for the main remote, and git clone sets it up for you.
  • upstream is the usual name for the original project when you've forked it.
  • A repository can have several remotes, for example your fork and the original project.

Remote-tracking branches

When you fetch from a remote, Git updates remote-tracking branches such as origin/main. They're read-only bookmarks showing where each branch was on the remote at your last fetch. You can look at them, compare with them and merge them, but you don't commit to them directly.

Your local main usually has origin/main set as its upstream. That relationship is what lets git status say:

Text
Your branch is ahead of 'origin/main' by 2 commits.

"Ahead" means you have commits the remote doesn't; "behind" means the remote has commits you haven't integrated yet. When both are true, the branches have diverged.

fetch versus pull

  • git fetch downloads new commits and updates origin/main, but doesn't touch your own branches or files. It's always safe.
  • git pull is git fetch followed by integrating origin/main into your current branch, by merging or, if configured, rebasing.

Fetching first lets you look before you combine: git log --oneline main..origin/main lists the commits waiting for you, and git diff main origin/main shows the changes. Many experienced developers fetch often and pull deliberately.

Pushing branches

Terminal
git push -u origin feature-login    # first push; set the upstream
git push                            # later pushes
git push origin --delete feature-login

-u (--set-upstream) links your local branch to the new remote branch, so plain git push and git pull know where to go afterwards. A push is rejected if the remote has commits you don't have; pull first, combine the work, then push again. Delete a remote branch once its pull request has been merged, and run git fetch --prune to remove stale remote-tracking branches from your list.

Git Remotes Syntax

Remote commands

git remote -v
git remote add <name> <url>
git fetch [--prune]
git pull
git push -u origin <branch>
git push origin --delete <branch>
git branch -vv               # show upstreams, ahead/behind
git log main..origin/main    # commits you don't have yet

Git Remotes Summary

  • A remote is a named address of another repository; origin is the usual main one.
  • Remote-tracking branches like origin/main record the remote's state at your last fetch.
  • git fetch downloads safely; git pull fetches and integrates into your branch.
  • git push -u origin branch publishes a branch and sets its upstream.
  • Ahead, behind and diverged describe your branch compared with its upstream.

Git Remotes Examples

Each example shows the code, the output it produces and what each part does.

Example 1

Fetch, look, then merge

Terminal
git fetch
git status
git log --oneline main..origin/main
git merge origin/main
Output
From https://github.com/samlee/portfolio
   9d41e07..b38e0f5  main       -> origin/main
On branch main
Your branch is behind 'origin/main' by 2 commits, and can be fast-forwarded.
b38e0f5 Update README with screenshots
a17c4d2 Fix footer link on mobile
Updating 9d41e07..b38e0f5
Fast-forward
 README.md   | 6 ++++++
 footer.html | 2 +-
 2 files changed, 7 insertions(+), 1 deletion(-)
Line-by-line explanation
  1. Line 1Download new commits; only origin/main moves.
  2. Line 2status compares main with its upstream and reports that you're behind.
  3. Line 3List exactly which commits you'd be bringing in.
  4. Line 4Integrate them. That's what git pull would have done in one step.
Example 2

Ahead, behind and upstreams at a glance

Terminal
git branch -vv
Output
  dark-mode  52ac9e1 [origin/dark-mode: ahead 1] Add dark colour scheme
* main       b38e0f5 [origin/main] Update README with screenshots
  spike      0f3e1a9 Try a new layout
Line-by-line explanation
  1. Line 1-vv shows each local branch, its upstream and how far ahead or behind it is. dark-mode has one commit to push, main is in sync, and spike has no upstream at all.
Example 3

Publishing and cleaning up a branch

Terminal
git switch -c footer-links
git commit -am "Add social links"
git push -u origin footer-links
# ...pull request merged on GitHub...
git switch main
git pull
git branch -d footer-links
git push origin --delete footer-links
git fetch --prune
Output
branch 'footer-links' set up to track 'origin/footer-links'.
Deleted branch footer-links (was a51f8c2).
To https://github.com/samlee/portfolio.git
 - [deleted]         footer-links
Line-by-line explanation
  1. Line 3The first push of a new branch uses -u to set its upstream.
  2. Lines 5 to 6After the merge, update main.
  3. Lines 7 to 8Delete the branch locally and on the remote.
  4. Line 9--prune removes remote-tracking branches that no longer exist on the server.

Common Git Remotes Mistakes

Thinking fetch updates your files

Don't
git fetch
# "why haven't my files changed?"
Do
git fetch
git merge origin/main     # or simply: git pull

Pushing a new branch without -u

Don't
git push
# fatal: The current branch footer-links has no upstream branch.
Do
git push -u origin footer-links

Force-pushing after a rejected push

Don't
git push --force
Do
git pull
git push

Git Remotes in Real Projects

Remotes in team workflows

Open-source contributors usually have two remotes: origin for their own fork and upstream for the original project, and they regularly git fetch upstream to stay current. Continuous integration servers clone and fetch repositories on every push, and deployment tools watch a remote branch such as main and release whatever is pushed there.

Git Remotes FAQ

How do I change the URL of a remote?

Run git remote set-url origin <new-url>, for example when a repository is renamed or you switch from HTTPS to SSH. Check the result with git remote -v. Your branches and history aren't affected.

What does "Your branch is ahead of 'origin/main' by 2 commits" mean?

You have two commits on your local main that the remote doesn't have yet. Run git push to send them. If it says "behind", the remote has commits you don't have, and git pull will bring them in.

How do I work on a branch that only exists on the remote?

Run git fetch, then git switch feature-name. If exactly one remote has a branch with that name, Git creates a local branch that tracks it, so git pull and git push work straight away. You don't need to create the branch yourself first.

Why does git branch -r still show branches deleted on GitHub?

Remote-tracking branches are only updated when you fetch, and a normal fetch doesn't remove them. Run git fetch --prune to delete the ones whose branches are gone. git config --global fetch.prune true makes every fetch do this automatically.

Can a repository have more than one remote?

Yes. Add another with git remote add backup <url> and refer to it by name, as in git push backup main or git fetch upstream. Contributors to open-source projects usually have origin for their own copy and upstream for the original project.

Git Remotes Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Fill in the blankseasy

Complete the command that downloads new commits without changing your files.

git 
Multiple choiceeasy

What is origin/main?

Multiple choicemedium

What does git pull do?

Predict the outputmedium

git status says "Your branch is ahead of 'origin/main' by 3 commits." What should you do to share them?

Multiple choicehard

What's safer than git push --force when you must update your own rebased branch?

Git Remotes Quiz

5 questions to check what you learned.

  1. Which command lists your remotes and their URLs?

    • git remote list
    • git remote -v
    • git origin
    • git show remotes
  2. What does "diverged" mean?

    • Your branch has been deleted
    • Both your branch and its upstream have commits the other doesn't have
    • You have no remote
    • The branch has conflicts
  3. What does -u do in git push -u origin feature?

    • Updates all branches
    • Undoes the push
    • Sets origin/feature as the upstream of your local feature branch
    • Uploads untracked files
  4. What does git fetch --prune do?

    • Removes remote-tracking branches that no longer exist on the remote
    • Deletes your local branches
    • Shrinks the repository
    • Removes old commits
  5. What does git log main..origin/main show?

    • Your commits that aren't pushed
    • Every commit in the repository
    • Commits on both branches
    • Commits on origin/main that your main doesn't have yet