Git and GitHub · Part 3 · GitHub and remote repositories

Git vs GitHub: how local and remote connect

Why put a repository online

So far, every repository has lived on one computer. If it breaks, the work is gone, and nobody else can see it. Putting a copy online fixes both. This lesson covers how that works, and practices it without GitHub first, to show what's really going on.

Git and GitHub

GitGitHub
What it isA tool on the computerA website and service
What it doesTracks history, branches and mergesStores Git repositories online, with team features
Needs internetNoYes
Made byLinus Torvalds, 2005GitHub, launched 2008, owned by Microsoft since 2018

GitHub doesn't replace Git: it runs Git on its servers and adds a website and team features on top, such as pull requests for reviewing changes, issues for tracking work, GitHub Actions for automation, and branch protection. GitLab, Bitbucket and Azure DevOps do the same job with the same Git underneath, so everything here works with all of them.

What a remote is

A remote is another copy of the repository, kept somewhere else, usually on a server like GitHub:

Local

Your computer

my-project/.git

push fetch / pull clone (once)

Remote (origin)

GitHub

the full history

Both are full repositories with the complete history. GitHub's copy isn't technically special; teams just agree to treat it as the official, shared one.

Distributed version control

Because everyone has the whole history, Git is called a distributed version control system:

Centralized (older tools like SVN)Distributed (Git)
Only the server has the full historyEvery copy has the full history
Committing and viewing history need the serverCommit, branch, log and diff all work offline
If the server is lost, the history is lostAny copy can restore everything

That's why every command so far worked without internet. Only syncing with a remote needs a connection.

The four ways to sync

CommandDirectionWhat it does
git cloneRemote → local, onceDownloads the whole repository for the first time
git pushLocal → remoteUploads new commits
git fetchRemote → localDownloads new commits, without changing any files
git pullRemote → localFetches, then merges into the current branch

origin: the remote's nickname

Every remote has a short name, so the full address doesn't have to be typed each time. The main one is called origin by convention:

origin  →  git@github.com:username/my-project.git

origin isn't a keyword, just the default name, like saving a contact as “Mom” instead of a phone number. A repository can have several remotes; with a fork, for example, origin is your copy and upstream is the original project.

origin/main: the last known position

Git keeps a local bookmark of where each remote branch was the last time it checked. These are remote-tracking branches, named like origin/main:

BranchMoved by
mainYour commits and merges
origin/mainOnly fetch, pull and push: it's where GitHub's main was at the last sync

origin/main is like a photo of the library's copy from the last visit. It doesn't update by itself; only another visit (git fetch) takes a new photo.

1.Ahead: local has commits the remote doesn't

git status
Your branch is ahead of 'origin/main' by 2 commits.
  (use "git push" to publish your local commits)
A
Borigin/main
C
Dmain

2.Behind: the remote has commits local doesn't

git status
Your branch is behind 'origin/main' by 3 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

3.Diverged: both have new commits

git status
Your branch and 'origin/main' have diverged,
and have 2 and 3 different commits each, respectively.
A
B
C
D
Gorigin/main
E
Fmain

The same split as two branches, fixed the same way: a merge (git pull) or a rebase (git pull --rebase).

A “GitHub” on your own computer

A remote can be a folder on the same computer. It behaves exactly like GitHub, without the website, so every idea above can be seen for real.

1.Create the “server”

Terminal
cd ~/Desktop
mkdir remote-lab
cd remote-lab
git init --bare fake-github.git

--bare makes a repository with no working files, only what's normally inside .git. That's what servers like GitHub keep. (Ending the name in .git is the convention.)

2.Create “your laptop” and connect it

Terminal
mkdir my-laptop
cd my-laptop
git init
echo "# Team Project" > README.md
git add README.md
git commit -m "Add README"

git remote add origin ../fake-github.git
git remote -v
Output
origin  ../fake-github.git (fetch)
origin  ../fake-github.git (push)

With GitHub, the address would be git@github.com:username/project.git instead of a folder. That's the only difference.

3.Push

Terminal
git push -u origin main
Output
To ../fake-github.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

This pushes main to the remote called origin. -u links the local main to origin/main, so after this, plain git push and git pull are enough. It's only needed the first time for each branch. git log --oneline --all now shows 7919ae4 (HEAD -> main, origin/main) Add README.

4.A teammate clones it

Terminal
cd ..
git clone fake-github.git teammate-laptop
cd teammate-laptop
git log --oneline
Output
7919ae4 (HEAD -> main, origin/main, origin/HEAD) Add README

git clone made the folder, downloaded the whole history, set up origin, and linked a local main to origin/main, all in one step.

5.The teammate pushes a change

Terminal
echo "Teammate was here" >> README.md
git commit -am "Add teammate note"
git push

No -u needed: cloning already set up the link.

6.Back on your laptop: behind without knowing

Terminal
cd ../my-laptop
git status
Output
Your branch is up to date with 'origin/main'.

It isn't: the photo is old. Take a new one:

Terminal
git fetch
git status
git log --oneline --all
Output
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.

ad73249 (origin/main, origin/HEAD) Add teammate note
7919ae4 (HEAD -> main) Add README

origin/main moved, but main and the files didn't: README.md doesn't have the teammate's line yet. Fetching only downloads; it never touches your work.

7.Bring it in

Terminal
git pull
Output
Updating 7919ae4..ad73249
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)

README.md now has the teammate's line. A pull is a fetch plus a merge, and this one was a fast-forward. origin/main is just another branch to merge from, so everything about branches applies to remotes too.

Remote commands

To…Run
List remotesgit remote -v
Add a remotegit remote add origin url
Change a remote's addressgit remote set-url origin new-url
Rename a remotegit remote rename old new
Remove a remotegit remote remove origin
List remote branchesgit branch -r
List local and remote branchesgit branch -a
See each branch's link and ahead/behindgit branch -vv

In my-laptop, git branch -vv shows * main ad73249 [origin/main] Add teammate note. The brackets name the linked remote branch, and show ahead 2 or behind 1 when they differ.

Public and private repositories

PublicPrivate
Who can see itAnyone on the internetOnly invited people
Good forOpen source and portfolio projectsClient and company code
Free on GitHubYesYes, with no limit on the number

Getting ready for GitHub

  • A free account at github.com.
  • Optional: the GitHub CLI, which creates repositories and pull requests from the terminal. On a Mac with Homebrew: brew install gh.