# 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.

Course: [Git & GitHub](https://devops.rajeev.pro/courses/git), lesson 4. About 25 minutes, beginner. Source: https://devops.rajeev.pro/courses/git/04-github-essentials

## What you will learn

- Explain what GitHub adds on top of Git
- Secure your account with two-factor authentication and keep your email address private
- Connect your machine with gh auth login or an SSH key
- Create a repository from the website or the command line, with a README, licence and .gitignore
- Choose between public and private, and know what each exposes
- Tell a fork from a clone, and pick the right one

## Git and GitHub

**The problem.** Git on your laptop keeps a perfect history, but only on your laptop. A team needs
one shared copy everyone can push to and pull from. Before hosted services, teams ran their own Git
server on a spare machine and emailed each other patches to review. Someone had to keep that server
backed up, patched and secure. There was nowhere to discuss a change next to the code, and nothing
ran the tests when someone pushed.

**What GitHub adds.** **Git** is the tool on your machine: commits, branches, history. It works with
no network at all. **GitHub** is a service that keeps the shared copy of a repository for your team,
and adds the work _around_ the code:

- **Pull requests:** propose a change, discuss it line by line, and merge it after review.
- **Issues:** track bugs and tasks next to the code that fixes them.
- **Actions:** run tests and deployments automatically on every push (the CI/CD course).
- **Releases and security scanning:** publish versions, and get warned about leaked secrets and
  vulnerable dependencies.

GitLab and Bitbucket do the same job, and the Git commands are identical on all three. Only the
website and the extras differ.

In this lesson you'll secure your account, connect your machine, create a repository, and learn the
difference between forking and cloning.

## Your account, safely

Your GitHub account can push code that other people deploy, so protect it like a production login:

1. **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.
2. **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:

```sh
$ git config --global user.email "12345678+rajeev@users.noreply.github.com"
```

3. **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`:

```sh
$ 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
```

`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:

```sh
$ 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)
```

Or start a fresh one on GitHub and clone it straight away:

```sh
$ 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.

> **Public is forever.** Bots scan every public push for passwords and API keys, often within minutes. Deleting the file in a new commit, rewriting history or switching the repository to private doesn't recall copies already made. If a secret was ever pushed, **revoke it and issue a new one**. The *Security* lesson shows how GitHub's secret scanning and push protection stop this before it happens.

## Fork or clone?

These two words get mixed up constantly. They do different things in different places:

> **Figure.** Fork versus clone. At the top, on GitHub: the original repository, owner/project, and your fork, you/project, a copy on GitHub that you can push to and that remembers where it came from. A fork happens on GitHub, server to server. Below, on your laptop: a clone, a full local copy with all history. You clone your fork; its remote origin points to your fork and upstream to the original. You push to origin, and open a pull request from your fork to the original. git fetch upstream keeps you up to date with the original.
>
> Fork: a copy on GitHub. Clone: a copy on your machine. Contributors do both.

|                   | 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.

```sh
$ 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
```

> **At work.** Use one GitHub account for work and personal projects, and add your work email to it: organisations grant access to your account, and you keep your history when you change jobs. Your company may require SSO, which links your account to the company login without creating a second account.

## Mission: 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.

- You need: A GitHub account with 2FA, and the shop repository/shop, private, with your commits"
>

```sh
$ 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
```

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

- Turn on two-factor authentication in GitHub Settings, under Password and authentication.
- In Settings, under Emails, turn on "Keep my email addresses private" and copy your noreply address.
- Install the GitHub CLI and run `gh auth login`, then `gh auth status`.
- Create a private test repository with `gh repo create test-repo --private --add-readme --clone`.
- Fork any public project on GitHub, then compare `git remote -v` in a clone of your fork.
- Create a repository named exactly like your username to get a profile README.

## Quiz

1. What is the difference between Git and GitHub?
   - They are the same thing
   - **Git is the version control tool on your machine; GitHub hosts repositories and adds collaboration: pull requests, issues, Actions** (answer)
   - GitHub is a newer version of Git
   Why: Git works fully offline. GitHub (like GitLab or Bitbucket) is a server for sharing repositories, plus the team tools around them.

2. How do you stop your personal email address appearing in public commits?
   - Use a fake email
   - **Turn on "Keep my email addresses private" and set your noreply address as user.email** (answer)
   - Make every repository private
   Why: Every commit carries the author's email, and public commits are public forever. GitHub gives you an ID+username@users.noreply.github.com address that still links commits to your profile.

3. You want to contribute to an open-source project you have no write access to. What do you do?
   - Clone it and push to it
   - **Fork it, clone your fork, push to your fork, then open a pull request to the original** (answer)
   - Ask for the owner's password
   Why: A fork is your own copy on GitHub that you can push to. The pull request asks the original project to take your changes.

4. You pushed an API key to a public repository and deleted it in the next commit. Is it safe now?
   - Yes, it is deleted
   - **No: it is still in history and may already be copied. Revoke and replace the key** (answer)
   - Yes, if you make the repository private
   Why: Bots scan public pushes for keys within minutes. Deleting or hiding it later does not recall copies. Rotating the key is the only fix.

5. What does a clone give you?
   - A copy on GitHub under your account
   - **A full local copy of the repository, with all history, linked to where it came from as origin** (answer)
   - Only the latest files
   Why: A clone is the whole repository on your machine. A fork is a copy on GitHub. You usually clone your fork.

## Resources

- [GitHub Docs: about GitHub and Git](https://docs.github.com/en/get-started/start-your-journey/about-github-and-git) (GitHub Docs)
- [GitHub Docs: setting your commit email address](https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-personal-account-on-github/managing-email-preferences/setting-your-commit-email-address) (GitHub Docs)
- [GitHub Docs: configuring two-factor authentication](https://docs.github.com/en/authentication/securing-your-account-with-two-factor-authentication-2fa/configuring-two-factor-authentication) (GitHub Docs)
- [GitHub Docs: fork a repository](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/fork-a-repo) (GitHub Docs)
- [GitHub CLI manual](https://cli.github.com/manual/) (GitHub)
- [Choose an open source licence](https://choosealicense.com/) (GitHub)
