Remotes, clone and origin

Beginner · 7 min

A remote is just a named URL for another copy of your repository. Learn what origin really is, what clone sets up, and how to add a remote by hand.

Before thisYour first repository and commitHow Git stores history

Anna Skoulikari · 7 min

Remotes, clone and origin

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Anna SkoulikariReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A remote is a short name for the URL of another copy of the repository, and `origin` is only the default name that `git clone` picks.
  • `git remote add <name> <url>` records a remote in `.git/config`, and `git remote -v` lists every remote with its fetch and push URLs.
  • `git clone` does four things: it creates a directory, copies the history, adds the `origin` remote, and checks out the default branch with tracking set up.
  • Remote-tracking branches such as `origin/main` are local bookmarks of where the remote's branches were when you last talked to it.
  • A remote can be a path on disk, an SSH address or an HTTPS URL, so you can practise everything in this group with a local bare repository.

The problem: your copy is the only copy

Everything so far lived in one directory on one laptop. If the disk dies, the history dies with it. If a teammate wants your commits, you would be emailing zip files. Git solves both with the same idea: every clone holds the whole history, and you tell Git where the other copies live.

Think of it like a phone’s contact list. A remote is a contact: a short name attached to an address. The name is for you. The address is for the network.

A remote is a name and a URL

To practise safely, make the “server” a bare repository on your own disk. A bare repository has the history but no working files, which is exactly what a hosting service keeps.

$ git init -q --bare hub.git
$ git init -q jot && cd jot
# ...two commits: jot.sh and a README...
$ git remote -v
$ git remote add origin ../hub.git
$ git remote -v
origin	../hub.git (fetch)
origin	../hub.git (push)
$ git push -u origin main
To ../hub.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

jot is the little notes CLI we grow through this group: a shell script that appends a line with jot add and prints numbered notes with jot list. The first git remote -v printed nothing, because a fresh repository has no remotes. After git remote add, there are two lines: one URL used when fetching and one when pushing. They are usually the same, and can differ, which comes up in forks and multiple remotes.

Look at what Git wrote to .git/config:

[remote "origin"]
	url = ../hub.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
	remote = origin
	merge = refs/heads/main

The url is the address. The fetch line is a rule saying “copy the remote’s branches into my refs/remotes/origin/ namespace”. The [branch "main"] block is the tracking relationship that -u created, covered in upstream tracking branches.

The URL can be a path like this one, an ssh:// address, or an https:// address. Git doesn’t care. The shape of every command stays the same.

What git clone does for you

Cloning a repository is a bundle of four steps you could do by hand:

$ git clone hub.git teammate
Cloning into 'teammate'...
done.
$ cd teammate
$ git branch -a
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main
$ git branch -vv
* main 5cf38a4 [origin/main] Add README
  1. It creates the directory and runs git init inside.
  2. It adds a remote named origin pointing at the URL you gave.
  3. It fetches all the history and creates a remote-tracking branch for each remote branch.
  4. It checks out the remote’s default branch as a local main that tracks origin/main.

(Hashes will differ on your machine. In my run git remote -v in the clone showed the absolute path of hub.git, because git clone stores the full path of a local source. The ../hub.git in the earlier listing is what I typed with git remote add.)

git remote show origin is the command to ask “what do I know about this remote?”. It lists the default branch, which remote branches are tracked and what git pull and git push will do.

origin is only a name

Nothing about origin is special. git clone picks it as a convention, and you can use git clone -o upstream <url> to pick another, or git remote rename origin hub afterwards. Tutorials say “push to origin” because that is what the name is on a fresh clone. When you work with a fork and a second remote, the name stops being something you can assume.

Remote-tracking branches are bookmarks

origin/main is not the remote’s branch. It is your local note of where that branch was the last time you talked to the remote. It only moves when you fetch or push. This single fact explains most “why doesn’t Git know about my teammate’s commit?” confusion, and the next lesson, fetch, pull and push, builds on it.

What the video covers

Anna Skoulikari’s “What is origin in Git (version control)? Why does it seem we have multiple remote repository names?” is about seven minutes, from around 2020. I judge English from the title, description and channel only, and I could not read a transcript, so this section rests on the title and description. It targets the exact confusion above: that origin is a name, not a place. The video is older, but remotes and git clone haven’t changed. A longer second view from Sachin Walia is linked below.

Where this fits

You can now connect a repository to another copy of itself. Next you will move commits across the connection: fetch, pull and push.

Check yourself

  1. 01Is `origin` a special name that Git treats differently from other remotes?

    No. It is only the name `git clone` gives the remote it cloned from. You can rename it, or add remotes with any other name.

  2. 02What does `git clone` create besides a copy of the files?

    It copies the full history, adds a remote called `origin` pointing at the source, creates remote-tracking branches, and checks out the default branch with its upstream set.

  3. 03Why does a bare repository make a good practice remote?

    It has no working tree, only the history, which is what a server needs. Pushing into a non-bare repository's checked-out branch is refused, so a bare one avoids that.

  4. 04You added a remote but `git branch -a` shows no `origin/...` branches. Why?

    Adding a remote only records its URL. The remote-tracking branches appear after the first `git fetch` or `git push` talks to it.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark