GitHub Get Started
So far everything happened in one folder on one computer. To back up your work and collaborate, you connect your repository to a remote: a copy on a server such as GitHub, GitLab or Bitbucket.
Creating a repository on GitHub
- Sign up at github.com (free) and verify your email.
- Click New (or the + menu → New repository).
- Choose a name, for example
shop, and Public or Private. - If you already have a local repository, don't tick "Add a README": an empty remote is easiest to push into.
- GitHub shows the repository URL, in one of two forms:
| Protocol | URL looks like | Authentication |
|---|---|---|
| HTTPS | https://github.com/ada/shop.git |
a personal access token or a credential manager (passwords are no longer accepted) |
| SSH | git@github.com:ada/shop.git |
an SSH key you add in Settings → SSH and GPG keys |
For SSH, create a key once and paste the public part into GitHub:
ssh-keygen -t ed25519 -C "ada@example.com"
cat ~/.ssh/id_ed25519.pub # copy this into GitHub
ssh -T git@github.com # "Hi ada! You've successfully authenticated…"How this course simulates GitHub
A remote does not have to be a website. Any bare repository (one without a working folder) can act as a server, including a folder on the same disk. The examples create one with git init --bare ~/github/shop.git; in real life, replace that path with your GitHub URL. Everything else is identical.
Connecting and pushing
git remote add <name> <url> stores the server address under a short name. By convention the main remote is called origin. Then git push -u origin main uploads the branch and remembers the link:
git init -q --bare ~/github/shop.git # stands in for "New repository" on GitHub
mkdir shop && cd shop && git init -q
echo "# Ada's Shop" > README.md
git add . && git commit -q -m "Add README"
git remote add origin ~/github/shop.git
git remote -v
git push -u origin mainorigin /home/learner/github/shop.git (fetch)
origin /home/learner/github/shop.git (push)
To /home/learner/github/shop.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
-u (short for --set-upstream) links local main to origin/main. From now on plain git push and git pull know where to go, and git status compares the two:
git init -q --bare ~/github/shop.git
mkdir shop && cd shop && git init -q
echo "# Ada's Shop" > README.md && git add . && git commit -q -m "Add README"
git remote add origin ~/github/shop.git
git push -q -u origin main
git status
echo "---"
echo "Opening hours: 9-17" >> README.md
git commit -q -am "Add opening hours"
git status
echo "---"
git push -q
git status -sbOn branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
---
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
---
## main...origin/main
"ahead of 'origin/main' by 1 commit" means you have a local commit the server doesn't have yet. After the push the branches are level again.
Checking what the server has
git ls-remote asks the remote which branches and tags it has. A fresh clone into another folder shows the files really arrived:
git init -q --bare ~/github/shop.git
mkdir shop && cd shop && git init -q
echo "# Ada's Shop" > README.md && git add . && git commit -q -m "Add README"
git remote add origin ~/github/shop.git
git push -q -u origin main
git ls-remote origin
cd ~
git clone -q ~/github/shop.git laptop-copy
cat laptop-copy/README.md
git -C laptop-copy log --oneline70a6496533278793b7a7857b98795973a83c27db HEAD
70a6496533278793b7a7857b98795973a83c27db refs/heads/main
# Ada's Shop
70a6496 Add README
git -C <folder> runs a Git command as if you had cd'd into that folder first.
Managing remotes
| Command | Does |
|---|---|
git remote -v |
list remotes with their URLs |
git remote add upstream <url> |
add another remote |
git remote rename origin github |
rename a remote |
git remote set-url origin <new-url> |
change the URL (e.g. switch from HTTPS to SSH) |
git remote remove upstream |
remove a remote |
Test yourself
What does the -u in git push -u origin main do?
- Pushes only updated files
- Sets origin/main as the upstream of local main, so later push/pull need no arguments
- Uploads untracked files
- Undoes the last push
git status says "Your branch is ahead of 'origin/main' by 2 commits". What should you do to share them?
- git pull
- git push
- git fetch
- Nothing, they are already on the server
Why does GitHub suggest an empty repository (no README) when you already have a local project?
- READMEs are not allowed
- So the remote has no unrelated commit and your first push succeeds without merging
- To save space
- Because private repositories can't have files