GitHub: your account, repositories, forks and clones
Set up a GitHub account safely, keep your email private, create repositories from the web or the gh CLI, choose public or private, and know exactly when to fork and when to clone.
By the end of this lesson you'll be able to
0 of 6 completeGit and GitHub
Git is the tool on your machine: commits, branches, history. It works with no network at all. GitHub is a service that hosts Git repositories so a team can share them, and adds the work around code: pull requests and reviews, issues, Actions for CI/CD, releases and security scanning. GitLab and Bitbucket do the same job; the Git commands are identical on all three.
Your account, safely
Your GitHub account can push code that other people deploy, so protect it like a production login:
- Two-factor authentication. GitHub requires it for anyone who contributes code. Use an authenticator app or a passkey, and save the recovery codes somewhere safe, not on the same laptop.
- Keep your email private. Every commit records the author's email, and public commits stay public
for good. In Settings → Emails, turn on Keep my email addresses private, and also Block command
line pushes that expose my email. GitHub gives you an address like
12345678+rajeev@users.noreply.github.com; set it as your email for public work:
$ git config --global user.email "12345678+rajeev@users.noreply.github.com"
git config --global user.email "12345678+rajeev@users.noreply.github.com"gitversion controlconfigread or set settings--globalfor your user, in every repo
- A profile people can read. Add your name, a photo and a short bio. Create a public repository named exactly like your username, and its README appears at the top of your profile: a good place for what you work on and links to your best projects.
Connecting your machine
Git needs to prove who you are when you push. The simplest way is the GitHub CLI, gh:
$ sudo apt install gh # or brew install gh, winget install GitHub.cli $ gh auth login # choose GitHub.com, HTTPS or SSH, log in with the browser $ gh auth status # who you're logged in as, and how
sudo apt install ghsudorun the next command as rootaptDebian/Ubuntu package manager
gh auth login stores a token and sets Git up to use it, so git push over HTTPS just works. If you
prefer SSH, make a key as in the Linux course's Networking lesson (ssh-keygen -t ed25519), add the
public key in Settings → SSH and GPG keys, and test it with ssh -T git@github.com. Passwords no
longer work for Git over HTTPS on GitHub; it's a token or SSH.
Creating a repository
On the website: the + menu, then New repository. Pick a name, a description, the visibility,
and optionally a README, a .gitignore template and a licence.
From the command line, for a project that already exists on your machine:
$ cd ~/projects/shop $ gh repo create shop --private --source=. --push # create on GitHub, add origin, push $ git remote -v origin https://github.com/rajeev/shop.git (fetch) origin https://github.com/rajeev/shop.git (push)
cd ~/projects/shopcdchange directory
git remote -vgitversion control
Or start a fresh one on GitHub and clone it straight away:
$ gh repo create notes --public --add-readme --license mit --gitignore Python --clone
| Starter file | Why |
|---|---|
README.md | the front page: what this is, how to run it, how to contribute |
.gitignore | keeps secrets and build output out from the first commit |
LICENSE | without one, nobody may legally reuse public code; MIT and Apache-2.0 are common |
Public or private?
| Public | Private | |
|---|---|---|
| Who can see it | everyone, and search engines | you and the people you invite |
| Who can push | only collaborators you add | only collaborators you add |
| Good for | open source, portfolio, docs | company code, anything with customers' details |
| Cost on the free plan | free | free, with unlimited collaborators |
Public means readable, not writable: strangers can't push to your repository, they can only fork it and ask you to pull their changes.
Fork or clone?
These two words get mixed up constantly. They do different things in different places:
| Fork | Clone | |
|---|---|---|
| Where the copy is | on GitHub, under your account | on your machine |
| Made with | the Fork button, or gh repo fork | git clone <url>, or gh repo clone |
| Use it when | you can't push to the original | you want to work on any repository locally |
- Your team's repository (you have write access): just clone it, branch, push, open a pull request.
- Someone else's project: fork it, clone your fork, push to your fork, open a pull request to the original. The Collaboration lesson walks through it.
$ gh repo fork owner/project --clone # fork on GitHub and clone your fork $ git remote -v # origin = your fork, upstream = the original $ git fetch upstream # get the original's newest commits
git remote -vgitversion control
Publish your project to GitHub
Put the shop repository from the earlier lessons on GitHub as a private repository, using your private noreply email, then check the push from both sides: GitHub and your terminal.
github.com/<you>/shop, private, with your commitsReveal one safe solution
$ gh auth login $ gh auth status $ cd ~/projects/shop $ git config user.email # make sure this is your noreply address $ gh repo create shop --private --source=. --push $ git status -sb # main is now tracking origin/main ## main...origin/main $ gh repo view --web # opens the repository in your browser
cd ~/projects/shopcdchange directory
git config user.emailgitversion controlconfigread or set settings
git status -sbstatuswhat changed
On GitHub, check the commit count matches git log --oneline | wc -l, and that .env isn't listed.
git status -sb showing main...origin/main with nothing after it means your local branch and
GitHub's are identical. The Remotes lesson explains what that line can tell you.
Try it yourself
0 of 6 steps doneRun each task in your own account or terminal, then tick it off.
Quick check
What is the difference between Git and GitHub?
Official reading for this lesson
Or get every quiz answer right and it completes itself.