Skip to content

GeneralGit from scratch

6. Remotes

Guide · git, version control · updated on 11/08/2026

Author: Ángel Fernández Caravaca
Date: August 11, 2026

Everything we have done so far has happened inside the .git folder on our machine. We can record work, read it, undo it, open parallel lines and integrate them, and none of it has ever left your disk. That is the restriction this chapter removes, since a repository is far more useful when it can talk to a copy of itself that lives somewhere else, e.g. on GitHub, on a colleague's laptop or in another folder on your own computer. In this chapter we will learn what a remote really is, what clone, fetch, pull and push actually move between the two sides, and why origin/main is a branch you can look at but never be on.

What is a remote?

The word suggests a place, and that is the first thing worth correcting. A remote is a name for a URL, that is, a bookmark Git keeps in .git/config so you don't have to type the other repository's address in every command. It contains no commits, no files and no history of its own. The repository at the other end of that URL does, but the remote itself is nothing more than a label:

origin  →  git@github.com:afdezcaravaca/git-from-scratch.git

The second thing worth correcting is the mental image of a "central server". Git is a distributed version control system, and that has a precise meaning here: the repository on GitHub is a repository exactly like yours, with its own objects/ and its own refs/, as described in Chapter 1. It is not a special kind of thing, nor is it the "real" copy of which yours is a partial view. Your clone contains the full history, so you can commit, create branches, merge and read the log with the network cable unplugged, and you will only notice the difference when you ask to synchronise.

That is why the operations in this chapter are so few. Since both sides are complete repositories, only three things can happen between them:

  • Downloading what the other side has and you don't, that is, git fetch.
  • Uploading what you have and the other side doesn't, that is, git push.
  • Copying everything the first time, that is, git clone.

Everything else in this chapter, git pull included, is one of those three operations combined with a command you already know from Chapters 4 and 5.

Note

The URL decides how the two repositories talk, that is, the transport and the authentication, and nothing else. It is the choice between HTTPS and SSH from Chapter 0, and it is worth repeating that it is a communication layer and not a content layer: git push moves exactly the same commits whichever one you configured. If a push fails with a permissions error, the problem is almost always there and not in anything this chapter describes.

Managing remotes: git remote

The family of commands that manipulates those labels is git remote, and the one you will type most is the listing:

git remote                 # just the names
git remote -v              # the names with their URLs, which is usually what you want
origin  git@github.com:afdezcaravaca/git-from-scratch.git (fetch)
origin  git@github.com:afdezcaravaca/git-from-scratch.git (push)

Two lines appear per remote because Git allows a different URL for reading and for writing, although in practice they are the same address and you can read the pair as a single entry. The rest of the family is small and predictable:

1
2
3
4
5
git remote add <name> <url>        # create the label
git remote rename <old> <new>      # rename it
git remote remove <name>           # delete the label, not the repository
git remote set-url <name> <url>    # point the same label at another address
git remote show origin             # a detailed report, which contacts the server

git remote remove deserves the emphasis it gets above. It deletes a line from your configuration and nothing else, so no commit is lost and the repository on the other side is not touched. The worst that can happen is having to add the label again.

The name origin is a convention, not a keyword. git clone uses it for the repository you cloned from simply because it has to call it something, and Git treats it like any other name. You can rename it, you can have several remotes at once, and in Chapter 7 you will do exactly that, since working with a fork means having one remote for your copy and another for the original project.

If you look at what git remote add actually wrote, you will find it in .git/config:

1
2
3
[remote "origin"]
    url = git@github.com:afdezcaravaca/git-from-scratch.git
    fetch = +refs/heads/*:refs/remotes/origin/*

The first line is the bookmark we have been describing. The second is the interesting one, and it is the whole subject of the next two sections: it says that every branch on the remote, that is, everything under its refs/heads/, must be copied into your repository under refs/remotes/origin/. Read that line as a rule about where downloaded branches land, and origin/main stops being a mystery.

Tip

If you cloned over HTTPS and later set up SSH, or the other way round, you don't need to clone again. git remote set-url origin <the other URL> is the only command that changes the channel, exactly as Chapter 0 showed, and your commits, branches and configuration stay where they are.

git clone: starting from a repository that already exists

Until now, every repository in this course started with git init in an empty folder. When the project already exists somewhere else, the command is git clone:

git clone git@github.com:afdezcaravaca/git-from-scratch.git
git clone git@github.com:afdezcaravaca/git-from-scratch.git my-folder   # choose the directory name

clone looks like a primitive operation, but it is really four things you already know, run one after another:

  1. git init in a new directory, that is, it creates the .git folder from Chapter 1.
  2. git remote add origin <url>, that is, it stores the address you typed under the name origin.
  3. git fetch, that is, it downloads every object and every branch from the remote, which is the section after this one.
  4. git switch <default branch>, that is, it creates a local branch that tracks the remote's default branch and checks it out, so you get files on disk instead of an empty folder.

Understanding that breakdown pays off immediately, because it explains why a freshly made clone already knows its remote, already has origin/main and already has one local branch and not the twenty that exist on the server. Nothing special has happened: they are simply the results of steps 2, 3 and 4.

Some options worth knowing:

1
2
3
4
git clone -b <branch> <url>      # check out that branch instead of the default one
git clone -o <name> <url>        # call the remote something other than origin
git clone --depth 1 <url>        # shallow clone: only the most recent commit
git clone --bare <url>           # no working directory, just the contents of .git
Option What it changes When it is useful
-b <branch> Leaves you on that branch after cloning The work you are after is not on the default branch
-o <name> Calls the remote something other than origin Forks, where upstream and origin are different repositories
--depth 1 Downloads a truncated history instead of the full one Huge repositories and CI jobs that only need the current state
--bare Creates a repository with no working directory Serving a repository, that is, what GitHub's copy looks like inside

Important

A normal clone is a complete copy of the history, so every clone of a project is also a complete backup of it. A shallow clone is not: with --depth 1 you get the current state and almost nothing behind it, so git log is practically empty and operations that need the past, e.g. git blame or comparing with an old commit, don't work. Use it when you want the files, not when you want the repository.

Remote-tracking branches: what origin/main really is

This is the concept that makes the rest of the chapter obvious, and the one most people carry the wrong picture of for years. There are three different things involved when we talk about "the main branch", and the vocabulary from Chapter 3 already gives us the tools to tell them apart:

Level Where it lives Who moves it
main .git/refs/heads/main, on your machine You, when you commit, merge or reset
origin/main .git/refs/remotes/origin/main, also on your machine Git, when you fetch, pull or push
main on the server refs/heads/main inside the GitHub repository Anyone who pushes to it, you included

The middle row is the one that matters. origin/main is a local file that records where the remote's main was the last time your repository talked to the server. It is a cache, stored on your disk like everything else, and it is not a live view: if a colleague pushes ten commits right now, your origin/main will keep pointing to the old commit until you run a command that contacts the server. Nothing about it updates on its own.

That single fact explains most of the confusing situations people run into:

  • git status saying you are up to date only means you match the last snapshot you downloaded, not that the server hasn't moved since.
  • A branch that "doesn't exist" is usually a branch that exists on the server and that you have simply never fetched.
  • origin/main moving when you push is not Git contradicting itself: you did contact the server, so the cache gets refreshed along the way.

You can list the three levels separately:

1
2
3
git branch          # local branches only
git branch -r       # remote-tracking branches only
git branch -a       # both at once
* main
  remotes/origin/main

The remaining question is what happens if you try to work on one of the remote ones:

git switch origin/main       # refuses, since it is not a local branch
git switch --detach origin/main   # works, and leaves you with a detached HEAD

You can go to the commit origin/main points to, but you land in the detached HEAD state from Chapter 2, that is, HEAD attached to a commit instead of a branch. Committing there creates commits no name points to, and the reason is not an arbitrary restriction: origin/main is the record of a fact about another repository, so Git won't let a local commit move it. If you want to work on top of what you have downloaded, you create a local branch from it:

git switch -c my-fix origin/main    # a local branch that starts where origin/main is
git switch main                     # or simply the local branch you already had

Note

This also clears up something that comes up when reading a graph. In git log --oneline --graph --all you will see origin/main drawn as just another label next to main, and that is exactly what it is: a pointer to a commit, of the same nature as a branch, that lives in a different refs/ folder and is updated by a different set of commands.

git fetch: downloading without touching your work

git fetch is the command that refreshes that cache, and it is the safest command in all of Git:

1
2
3
4
git fetch                  # from the current branch's remote, usually origin
git fetch origin           # explicitly from origin
git fetch --all            # from every remote you have configured
git fetch origin main      # only that branch

What it does is concrete and precise. It contacts the remote, downloads the objects you are missing, writes them into objects/ exactly as Chapter 1 described and moves the pointers in refs/remotes/. What it does not do is the important half:

  • It doesn't touch your working directory, so no file on disk changes.
  • It doesn't touch the staging area, so nothing is staged or unstaged.
  • It doesn't move any local branch, so main stays exactly where you left it.
  • It can't produce a conflict, since nothing is being combined.

In other words, after a fetch you know what the other side has done, and you haven't decided anything about it yet. That is why fetch can be run at any time, even in the middle of unfinished work with a dirty working directory.

The natural next step is to look at what has arrived, and the range notation from Chapter 2 is what does it:

1
2
3
4
git log --oneline main..origin/main    # commits they have and you don't
git log --oneline origin/main..main    # commits you have and they don't
git diff main origin/main              # the accumulated difference in content
git log --oneline --graph --all        # the full picture, with both labels drawn

Read A..B as reachable from B but not from A, that is, exactly the question you want to answer before deciding whether to integrate. The first line is "how far behind am I", the second is "what haven't I published yet", and running them in that order before a pull turns synchronising into an informed decision instead of a leap in the dark.

git fetch --dry-run        # report what would be downloaded, without downloading anything
git fetch --prune          # also delete cached refs whose branch no longer exists on the server

Tip

If you keep only one habit from this chapter, make it this one: git fetch first, look, and then decide. It takes a second, it can never break anything and it replaces the most common source of surprises, that is, running git pull and finding out what the other side did when it is already merged into your branch.

git pull: fetch plus integration

git pull is the command everyone learns first and the one that causes the most confusion, and the confusion disappears as soon as you know it is not a primitive operation. git pull is two commands dressed up as one:

1
2
3
git pull                         # is equivalent to:
git fetch                        #   1. download and update origin/main
git merge origin/main            #   2. integrate that into the branch you are on

That second step is the git merge from Chapter 4, unchanged and with no special case for remotes. Everything you learnt there applies to the letter:

  • If your branch hasn't moved since the last sync, the merge is a fast-forward, that is, your main slides to where origin/main is and no commit is created. This is the usual case, which is why a pull normally looks like it did nothing.
  • If both sides moved, that is, you made local commits while someone else pushed, the merge writes a merge commit with two parents, and your history forks and joins again.
  • If both sides changed the same region of the same file, you get a conflict, with the markers and the resolution cycle from Chapter 5. A "pull conflict" is not a different kind of conflict, so git status, git add and git merge --abort are still the commands that get you out of it.

The variants are worth configuring once instead of remembering them:

1
2
3
4
git pull --rebase          # replay your local commits on top instead of merging
git pull --ff-only         # accept only a fast-forward; refuse if the branches diverged
git config --global pull.ff only      # make --ff-only the default behaviour
git config --global pull.rebase true  # make --rebase the default behaviour

--ff-only is the conservative option and a good default while you are learning, since it never writes a merge commit behind your back: when the two sides have truly diverged, it stops and tells you, and you decide what to do with all the information in front of you. --rebase produces a linear history instead of a merge commit, and it is genuinely useful, but it rewrites your local commits, and that has consequences we are not ready for yet. Chapter 8 is entirely about it.

Warning

Pulling with uncommitted changes in the working directory is where people get hurt. If the incoming commits touch a file you have modified but not committed, Git refuses and asks you to commit or stash first, which is the good case. If they touch other files, the merge goes ahead and your uncommitted work ends up mixed with someone else's changes in a state that git merge --abort can't fully restore, since, as Chapter 5 insisted, only what has been committed is safe. Run git status before pulling, exactly as before every operation that rewrites your files.

git push: publishing your commits

push is the mirror image of fetch, that is, it uploads the objects the remote is missing and then asks it to move one of its branch pointers:

1
2
3
4
5
6
git push                          # push the current branch to its remote counterpart
git push origin main              # explicit about remote and branch
git push -u origin main           # the same, and remember the pair for next time
git push origin --delete feature  # delete that branch on the remote
git push --all origin             # push every local branch
git push --tags                   # push the tags, which are the subject of Chapter 9

The mechanics are what you would expect after the previous sections: Git works out which objects reachable from your tip the remote doesn't have, sends them and requests that refs/heads/main on the server be rewritten with your commit's hash. If the server accepts, your origin/main is updated too, since you have just learnt something reliable about the other side.

The interesting part is when the server refuses. There is exactly one rule, and it is the rule from Chapter 4 applied remotely: a push is only accepted if it is a fast-forward on the remote side, that is, only if the commit the remote branch now points to is an ancestor of the commit you are pushing. If someone pushed while you were working, that is no longer true and Git stops:

1
2
3
4
5
6
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'github.com:afdezcaravaca/git-from-scratch.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing to
hint: the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.

This is not a failure, it is a safeguard, and it is protecting someone else's commits: if Git moved the pointer anyway, the commits that were on the server and not in your history would no longer be reachable from any branch. The fix is the cycle you already have all the pieces for:

1
2
3
4
git fetch                    # 1. see what they did
git log --oneline main..origin/main   # 2. read it before integrating
git merge origin/main        # 3. integrate it, resolving conflicts if there are any
git push                     # 4. now your commit is a descendant, so it is accepted

Caution

There is a way to tell the server to move the pointer anyway, and it is git push --force. It discards everything on the remote branch that is not in your history, that is, it does to a shared branch exactly what the rejection was preventing. If you really need it, e.g. after the rebases in Chapter 8, use git push --force-with-lease instead: it forces the update only if the remote is still where your last fetch said it was, so it refuses when someone pushed in the meantime. --force overwrites blindly; --force-with-lease overwrites only what you have actually seen.

Note

Deleting a remote branch with git push origin --delete feature removes the name on the server, not the commits, and it doesn't touch your local feature either. It is the remote equivalent of the git branch -d clean-up from Chapter 4, and after a merged feature branch it is the second half of that same clean-up.

Upstream: pairing a local branch with a remote one

We have been typing git push and git pull without arguments without saying anything, and it is time to explain why it works. A local branch can record which remote branch it corresponds to, and that pairing is called its upstream. It is stored, like everything else, in .git/config:

1
2
3
[branch "main"]
    remote = origin
    merge = refs/heads/main

The link gives you two concrete things. First, the argument-less forms of the commands know where to go, so git push and git pull need no arguments. Second, and more useful day to day, git status can compare the two and tell you how far apart they are:

git branch -vv       # each local branch with its upstream and its position
* main 0f2bb7e [origin/main] Chapter 05

git clone sets this up on its own for the default branch, which is why a freshly made clone can pull straight away. A branch you created locally has no upstream until you assign one:

1
2
3
git push -u origin feature                  # push and set the upstream in one go
git branch --set-upstream-to=origin/feature # set it without pushing anything
git branch --unset-upstream                 # remove the pairing

-u is the flag worth building a habit around: use it on the first push of every new branch and never think about it again.

Once the pairing exists, git status starts reporting a relationship, and there are only four possible ones. Bear in mind that all four are computed against your cached origin/main, so they describe the last snapshot you downloaded and are only as recent as your last fetch:

What git status says What it means What to do
Your branch is up to date Both pointers are on the same commit Nothing
Your branch is ahead by N commits You have unpublished commits git push
Your branch is behind by N commits They have commits you haven't integrated git pull
Your branch and origin/main have diverged Both sides moved since you last matched git pull (merge or rebase), then git push

Two more compact ways to read the same thing:

git status -sb                                       # just the branch line
git rev-list --left-right --count main...origin/main # two numbers: ahead, behind

Important

The upstream link is a configuration setting, not a permanent property of the branch, and it doesn't turn the two branches into the same branch. main and origin/main are still two independent pointers that simply know about each other, which is precisely why they can diverge and why the table above needs four rows instead of one.

Keeping the picture accurate: stale references

One asymmetry remains. When a branch is deleted on the server, e.g. because GitHub removed it after merging a pull request, your refs/remotes/origin/ keeps its copy of that pointer indefinitely. Nothing tells your repository that the branch is gone, so git branch -r slowly fills up with names that no longer exist anywhere:

1
2
3
4
git remote prune origin              # delete cached refs whose branch no longer exists
git fetch --prune                    # fetch and clean up in a single command
git remote prune origin --dry-run    # list what would be deleted, without deleting anything
git config --global fetch.prune true # clean up on every fetch, from now on

The last line is the one worth running once and forgetting about, since a stale origin/feature is never information you want.

Note

Pruning deletes only the cached pointer. It doesn't delete any commit, it doesn't touch any local branch with the same name and it can't affect the remote repository. If a local feature tracked an origin/feature that has since been deleted, the local branch stays exactly where it is, with all its commits, and simply loses its upstream.

Summary

Command What it does
git remote -v Lists the configured remotes with their URLs
git remote add <name> <url> Creates a name for a remote repository
git remote set-url origin <url> Points an existing remote at another address
git remote remove <name> Deletes the label, without touching either repository
git remote show origin Contacts the server and reports the state of each branch
git clone <url> Copies a whole repository, setting up origin and the default branch
git clone --depth 1 <url> Shallow clone, that is, the current state without the history
git branch -r / git branch -a Lists the remote-tracking branches, or all of them
git fetch Downloads objects and updates origin/*, without changing anything else
git fetch --all / --prune / --dry-run Every remote / also remove stale refs / just report
git log --oneline main..origin/main The commits they have and you don't
git log --oneline origin/main..main The commits you have and they don't
git pull git fetch followed by git merge origin/<branch>
git pull --ff-only Pulls only if it is a fast-forward; refuses if the branches diverged
git pull --rebase Replays your local commits on top instead of merging (Chapter 8)
git push Uploads your commits and moves the branch pointer on the remote
git push -u origin <branch> The same, and records the pairing with the upstream
git push origin --delete <branch> Deletes a branch on the remote
git push --force-with-lease Forces the update, but only if the remote is where you last saw it
git branch -vv Shows each local branch with its upstream and how far apart they are
git branch --set-upstream-to=origin/<branch> Pairs a local branch with a remote one
git status -sb The ahead/behind summary on a single line
git remote prune origin Removes cached references to branches deleted on the server

Your repository is no longer confined to one machine. It can be copied, kept in sync with a copy of itself and published, and all of it has turned out to be fetch, push and the merge you already knew. What we have described, however, is still a single person pushing to a repository they have write access to, which is not how contributing to someone else's project works. That is where Chapter 7 goes: forking a repository you can't push to, proposing your work as a pull request, getting it reviewed, and the day-to-day cycle known as GitHub Flow.