Learn GitHub
$ gh ghfoundations.ghrepos

GitHub Repositories

6 learning objectives · 9 key terms · 8 commands · 3 diagrams · 5 quiz questions

$objectives

GitHub Repositories

Foundations

Module 1 of 5 · lesson 3 in this module

What you'll be able to do

  • Create a GitHub repository with a sensible name, description and visibility
  • Choose between public and private repositories, and know when to change your mind
  • Use README, license and .gitignore from the start so the project explains itself
  • Clone a remote repository and connect an existing local one
  • Push local commits up and pull teammates’ commits down safely
  • Distinguish git fetch, git pull and git push precisely
$concept

The idea, in plain language

A GitHub repository is a Git repository plus the context a stranger needs: a name, a one-line description, a README that renders on the front page, a topic list, and permission settings that decide who can read or write. When you create one in the browser, GitHub can supply the three files a healthy project starts with — a README, a .gitignore matching your language, and a license — and it makes an initial commit on your behalf. Because that first commit exists on GitHub and not on your laptop, the local project you already have and the remote you just created need to be connected deliberately.

Connecting them is a two-line job: register the remote with a name (by strong convention, origin) and a URL, then push your branch and set its upstream so later pushes and pulls know where to go. From then on the remote is just another copy of the same repository that happens to live on a server. Your local branch and its remote-tracking counterpart move independently — you can accumulate five local commits while GitHub still shows the old state, and nothing is inconsistent about that; git status simply reports you are ahead.

That independence is the key to the three sync commands. git fetch downloads new objects and updates your remote-tracking branches but never edits your files, so it is always safe. git pull is git fetch followed by an integration step (merge by default, rebase if you configure it), so it does change your working directory and can produce conflicts. git push uploads your local commits and is the only one of the three that changes what other people can see.

$key terms

Key terms

origin
The conventional name for the remote you cloned from or push your work to.
upstream branch
The remote branch a local branch tracks, so plain git push and git pull know where to go.
Remote-tracking branch
Your local record of where a remote branch stood the last time you fetched, such as origin/main.
Clone
A full copy of a remote repository, including history, plus the origin remote configured for you.
README.md
The Markdown file GitHub renders on the repository home page — the project’s front door.
License
The file that states what others are legally allowed to do with your code.
Visibility
Public (anyone can read) or private (only invited collaborators can).
Ahead / behind
How many commits your local branch has that the remote does not, and vice versa.
Personal access token
A scoped password replacement used for HTTPS authentication.
$steps

How it works, step by step

  1. 1
    Create the repository on GitHub

    Name it in lowercase with hyphens, write a one-line description (it appears in search and on your profile), and pick visibility. Tick "Add a README file", "Add .gitignore" and "Choose a license" — these three files on day one save hours later.

    or the browser, or the CLIbash
    gh repo create recipe-book --public --description "A tiny recipe manager" --clone
  2. 2
    Clone it (new project) or connect it (existing project)

    Cloning gives you the repository plus the origin remote already wired up. If you already have local commits, add the remote instead and push.

    new projectbash
    git clone git@github.com:ada/recipe-book.git
    cd recipe-book
  3. 3
    Point an existing repository at GitHub

    Register the remote, make sure your branch is called main, then push and set the upstream so future pushes need no arguments.

    existing projectbash
    git remote add origin git@github.com:ada/recipe-book.git
    git branch -M main
    git push -u origin main
  4. 4
    Confirm the wiring

    git remote -v prints each remote with its fetch and push URLs. If the URL is wrong, nothing else will work — fix it with git remote set-url.

    verifybash
    git remote -v
  5. 5
    Fetch to look, pull to integrate

    Run git fetch to see what changed on the remote without touching your files, then git pull when you are ready to actually bring those commits into your branch.

    keep up to datebash
    git fetch origin
    git status
    git pull origin main
  6. 6
    Push your work

    After the first push with -u, plain git push sends the current branch to its upstream. Pushing is publishing: everything you push is visible to whoever can read the repository.

    publishbash
    git push
$commands

Commands worth memorising

Every block below is copy-ready — use the Copy button, then paste it into your terminal. Expected output is shown underneath so you know what “working” looks like.

clonebash
git clone git@github.com:ada/recipe-book.git
Output
Cloning into 'recipe-book'...
remote: Enumerating objects: 12, done.
Receiving objects: 100% (12/12), done.

Downloads the full repository and configures origin for you.

list remotesbash
git remote -v
Output
origin	git@github.com:ada/recipe-book.git (fetch)
origin	git@github.com:ada/recipe-book.git (push)

Run this whenever a push goes somewhere unexpected.

add a remotebash
git remote add origin <repository-url>

Connects a repository you created locally to one you created on GitHub.

fix a wrong URLbash
git remote set-url origin <new-repository-url>

Renamed the repository on GitHub? Update the URL rather than removing and re-adding the remote.

push and set upstreambash
git push -u origin main
Output
branch 'main' set up to track 'origin/main'.
To github.com:ada/recipe-book.git
 * [new branch]      main -> main

The -u is needed once per branch; afterwards plain git push and git pull work.

look without touchingbash
git fetch origin --prune

Downloads new commits and drops remote-tracking branches that no longer exist. Never modifies your working files.

integratebash
git pull --rebase origin main

Fetch, then replay your local commits on top of the remote ones — keeps history linear.

see where you standbash
git status -sb
Output
## main...origin/main [ahead 2]

Ahead 2 means two local commits are waiting to be pushed; behind means you should pull.

$diagrams

Workflow diagrams

Diagram 1Local repository and remote repository, in sync and apart

The two copies move independently. Only push, fetch and pull cross the boundary.

Reading the diagram
  1. git push sends local commits to GitHub and is the only command that changes what others see.
  2. git fetch downloads new remote commits into your remote-tracking branches without touching your files.
  3. git pull is fetch plus merge or rebase, so it does change your working directory and can conflict.
  4. Committing locally while the remote is behind is normal — git status shows you are ahead, not broken.
Diagram 2fetch, pull and push compared

The direction is what matters: these three commands are the entire network surface of daily Git work.

Reading the diagram
  1. fetch is the safe read-only glance at the remote.
  2. pull is fetch plus integration, which is why it can raise merge conflicts.
  3. push publishes; it fails if the remote has commits you have not integrated yet.
Diagram 3Connecting a local project to a new GitHub repository

The order matters. Pushing into a remote that has its own commits is rejected, and that rejection is Git protecting your history.

Reading the diagram
  1. If you ticked "Add a README" when creating the repository, GitHub already has a commit your clone does not know about.
  2. Pull with rebase first so your local commits are replayed on top of it, then push.
  3. If the repository is completely empty, push straight away with git push -u origin main.
$example

Worked example

The full path from an empty GitHub repository to a synced pair, using the terminal the whole way.

publishing an existing projectbash
cd recipe-book
git remote add origin git@github.com:ada/recipe-book.git
git branch -M main
git push -u origin main
# To github.com:ada/recipe-book.git
#  * [new branch]      main -> main
# branch 'main' set up to track 'origin/main'.

git remote -v
# origin  git@github.com:ada/recipe-book.git (fetch)
# origin  git@github.com:ada/recipe-book.git (push)

git status -sb
# ## main...origin/main

# a teammate pushes a commit; check before integrating
git fetch origin
# remote: Enumerating objects: 5, done.
git status -sb
# ## main...origin/main [behind 1]

git pull --rebase
# Successfully rebased and updated refs/heads/main.
Output
Your local branch now tracks origin/main, and the repository is visible on GitHub with the full history.
1git remote add origin gives the GitHub URL a short local name. Nothing is uploaded yet — this only records where "origin" is.
2git branch -M main renames the current branch to main if it is called something else, matching GitHub’s default so the two sides agree.
3The first push carries every local commit up and, thanks to -u, records that main should track origin/main. Later pushes need no arguments.
4git remote -v confirms both URLs. If this shows the wrong account or protocol, fix it now with git remote set-url.
5git status -sb prints a compact status: three lines in, the branch line "## main...origin/main" means the two sides match. [behind 1] later means one commit exists on GitHub that you do not have.
6git fetch downloads it without touching your files, so you can inspect it first. git pull --rebase then replays your own commits on top, keeping history linear.
$use cases

Where you'll use it

▸Publishing a portfolio repository that recruiters can read without any setup.
▸Cloning a teammate’s repository to reproduce a bug on your machine.
▸Keeping a private repository for client work while your public profile stays open.
▸Adding a license so companies can legally use your library.
▸Sharing a single source of truth for documentation that renders nicely on GitHub.
▸Backing up work with push, so a broken laptop costs you nothing.
$mistakes

Common mistakes

  • Creating the GitHub repository with a README and then trying to push a local history that has no common ancestor — the push is rejected and looks broken.
  • Working for days without pushing, then discovering a teammate built a completely different solution.
  • Committing a client’s secrets into a public repository — visibility is public forever, even after deletion.
  • Using git pull blindly on a branch with local uncommitted changes and ending up mid-merge with no idea how to finish.
  • Pulling instead of fetching when you only wanted to look, then having to unwind an unwanted merge.
  • Forgetting the -u on the first push and then wondering why plain git push complains about no upstream.
$best practices

Best practices

  • Give every repository a one-line description and a README that says what it is, why it exists and how to run it.
  • Add a license deliberately — its absence means "all rights reserved" whether you intended that or not.
  • Fetch before you pull when you are unsure; the worst case of fetching is nothing at all.
  • Push at least at the end of every working session so the remote is never far behind reality.
  • Use SSH keys or a personal access token instead of typing a password, and keep those credentials out of the repository.
  • Name repositories in lowercase-with-hyphens so URLs, clones and imports stay predictable.
$exercise

Hands-on exercise

Publish a local repository and practise the three sync commands

Connect a repository you already have to GitHub and move commits in both directions.

  1. On GitHub, create a repository named git-practice with a description and a README, and set it to public.
  2. Clone it, then add a file and commit it locally. Push and confirm the file appears on GitHub.
  3. Without pushing, make two more local commits, then run git status -sb and read the ahead count.
  4. On GitHub, edit the README in the browser and commit the change there — this simulates a teammate.
  5. Back in the terminal run git fetch, then git status -sb, and note that your files have not changed even though Git now knows about the new remote commit.
  6. Run git pull --rebase, then git log --oneline --graph, and push, so both copies match again.

💡 Hint: If the push after step 2 is rejected with "updates were rejected because the remote contains work that you do not have locally", fetch and rebase before pushing.

Done when: git status -sb prints ## main...origin/main with no ahead or behind count, and GitHub shows every commit you made locally.

$quiz

Checkpoint quiz

0 of 5 answered · 0 correct

  1. Multiple choiceQ1

    Which command downloads new commits from GitHub without changing your working files?

  2. Command outputQ2

    What does this status line mean?

    ## main...origin/main [ahead 3]
  3. TroubleshootingQ3

    Your first push to a brand-new GitHub repository is rejected, saying the remote contains work you do not have locally. The repository was created with a README. What is the cleanest fix?

  4. True or falseQ4

    True or false: a repository can be public on GitHub with no license, which means others may reuse the code freely.

  5. Multiple choiceQ5

    What does the -u flag on git push -u origin main do?

$summary

Module summary

  • A GitHub repository = a Git repository plus README, description, license, visibility and permissions.
  • Cloning sets up origin for you; connecting an existing project needs git remote add plus a first push with -u.
  • git remote -v is the first thing to check when pushes go somewhere unexpected.
  • fetch looks, pull integrates, push publishes — only push changes what other people see.
  • Local and remote histories can legitimately differ; git status -sb tells you which way to reconcile.
  • Visibility is a security decision, and a license is a legal one — both belong on day one.

Next up: Your project is online. Module 4 leaves main alone and works the way professionals do: one short-lived branch per change, merged back through the GitHub Flow.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Put the GitHub course to work: open one of the company-wise repositories and follow the workflow you just learned, branch to release.