Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 36 ยท Getting Started

Creating Your First Repository

Create your first GitHub repository on the website, clone it, make a commit and push it, or push an existing local project. Learn what each step does and the usual errors.

There are two common starting points: you begin a new project on GitHub and clone it, or you already have a folder on your computer and want to publish it. Both end the same way, with a local repository whose origin remote points at GitHub. This lesson walks through both and explains the messages Git prints so they stop being mysterious.

It assumes Git is installed (git --version) and your name and email are configured, as in Your GitHub Account and Profile.

Path 1: start on GitHub

  1. Click the + menu at the top right, then New repository.
  2. Choose an owner (you or an organization) and a repository name. Names can contain letters, numbers, hyphens, underscores and dots. Pick short, lowercase names such as weather-cli.
  3. Add an optional description.
  4. Choose Public or Private. You can change visibility later.
  5. Optionally tick Add a README file, choose a .gitignore template for your language, and pick a license. Each of these is covered in README, License and .gitignore.
  6. Click Create repository.

Then copy the clone URL (the green Code button) and run:

Terminal
git clone https://github.com/YOUR-NAME/weather-cli.git
cd weather-cli
git status

git clone downloads all history, creates a folder, checks out the default branch (main), and registers a remote named origin. git status should say On branch main and Your branch is up to date with 'origin/main'.

Now make a change and send it back:

Terminal
echo "Prints the weather for a city." >> README.md
git add README.md
git commit -m "Describe what the tool does"
git push

Refresh the repository page and your commit is there. Because the clone set up tracking, a plain git push knows where to go.

Path 2: publish an existing project

Suppose you already have a folder with code. Turn it into a repository, create an empty repository on GitHub (leave the README, .gitignore and license boxes unticked so there is nothing to conflict with), and connect them:

Terminal
cd my-project
git init -b main
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/YOUR-NAME/my-project.git
git push -u origin main

The -u flag (--set-upstream) records that your local main tracks origin/main, so later git push and git pull need no arguments.

Before git add ., create a .gitignore so you do not commit node_modules, build output, .env files or secrets. Removing a committed secret from history is much harder than not committing it.

What the remote commands really do

sequenceDiagram
  participant L as Local repo
  participant G as GitHub (origin)
  L->>L: git commit (saved locally only)
  L->>G: git push origin main
  G-->>L: updates origin/main to match
  Note over L,G: origin/main on your machine is a bookmark of GitHub's state

The name origin/main on your computer is a remote-tracking branch: your last known copy of GitHub's main.

git remote -v lists your remotes with their URLs. If you pasted the wrong URL you can fix it with git remote set-url origin NEW-URL.

Common errors and what they mean

You can model the most common push outcome with a small stand-in. The rule real Git uses is simple: a push is accepted only if it is a fast-forward, meaning the remote branch's tip is already contained in your history.

JS Stand-in: will my push be accepted?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The second case is the famous message:

TEXT
! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/...'

It is not a disaster; it means GitHub has commits you lack. Run git pull (or git pull --rebase), resolve any conflicts, and push again. Never "fix" it with git push --force on a shared branch unless you understand what history you are throwing away.

Other frequent problems:

Message Cause Fix
remote: Repository not found Wrong URL, or no access to a private repo Check git remote -v and your sign-in
Authentication failed / password no longer supported GitHub does not accept account passwords over HTTPS Use a token, a credential manager or SSH (see Authentication)
src refspec main does not match any You have no commits yet, or your branch is named differently Commit first; check git branch
refusing to merge unrelated histories The GitHub repo had its own initial commit Create the GitHub repo empty, or use git pull --allow-unrelated-histories once

Editing on the website

You do not need Git for small fixes. On any file, the pencil icon opens an editor, and Commit changes can commit straight to a branch or open a pull request. Pressing . opens the whole repository in a browser-based VS Code editor. This is ideal for typos in documentation, but for anything that needs running or testing, work locally.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the -u option in git push -u origin main do?

    -u is --set-upstream. It stores the tracking relationship.
  2. 2. You are publishing an existing local project. How should the new GitHub repository be created?

    An empty remote avoids unrelated histories and conflicts with your first push.
  3. 3. A push is rejected with 'fetch first'. What happened?

    The remote branch moved. Integrate its commits, then push.
  4. 4. What is origin/main on your computer?

    It is a remote-tracking branch, updated by fetch, pull and push.

Summary

  • Create a repository from the + menu, then git clone it; or git init, git remote add origin and git push -u origin main for an existing project.
  • origin is the default name of the GitHub remote; origin/main is your bookmark of its state.
  • A push succeeds only as a fast-forward; otherwise pull first.
  • Add a .gitignore before the first commit, and never commit secrets.

Next: Authentication: SSH Keys and Tokens