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
| Git | GitHub | |
|---|---|---|
| What it is | A tool on the computer | A website and service |
| What it does | Tracks history, branches and merges | Stores Git repositories online, with team features |
| Needs internet | No | Yes |
| Made by | Linus Torvalds, 2005 | GitHub, 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
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 history | Every copy has the full history |
| Committing and viewing history need the server | Commit, branch, log and diff all work offline |
| If the server is lost, the history is lost | Any 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
| Command | Direction | What it does |
|---|---|---|
git clone | Remote → local, once | Downloads the whole repository for the first time |
git push | Local → remote | Uploads new commits |
git fetch | Remote → local | Downloads new commits, without changing any files |
git pull | Remote → local | Fetches, 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.gitorigin 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:
| Branch | Moved by |
|---|---|
main | Your commits and merges |
origin/main | Only 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
Your branch is ahead of 'origin/main' by 2 commits.
(use "git push" to publish your local commits)2.Behind: the remote has commits local doesn't
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
Your branch and 'origin/main' have diverged,
and have 2 and 3 different commits each, respectively.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”
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
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 -vorigin ../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
git push -u origin mainTo ../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
cd ..
git clone fake-github.git teammate-laptop
cd teammate-laptop
git log --oneline7919ae4 (HEAD -> main, origin/main, origin/HEAD) Add READMEgit 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
echo "Teammate was here" >> README.md
git commit -am "Add teammate note"
git pushNo -u needed: cloning already set up the link.
6.Back on your laptop: behind without knowing
cd ../my-laptop
git statusYour branch is up to date with 'origin/main'.It isn't: the photo is old. Take a new one:
git fetch
git status
git log --oneline --allYour 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 READMEorigin/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
git pullUpdating 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 remotes | git remote -v |
| Add a remote | git remote add origin url |
| Change a remote's address | git remote set-url origin new-url |
| Rename a remote | git remote rename old new |
| Remove a remote | git remote remove origin |
| List remote branches | git branch -r |
| List local and remote branches | git branch -a |
| See each branch's link and ahead/behind | git 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
| Public | Private | |
|---|---|---|
| Who can see it | Anyone on the internet | Only invited people |
| Good for | Open source and portfolio projects | Client and company code |
| Free on GitHub | Yes | Yes, 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.