Creating and publishing a repository

Beginner · 10 min

Turn a local Git project into a GitHub repository: create an empty remote, connect it with git remote add, push main, and choose visibility, license and README.

Before thisYour first repository and commitRemotes, clone and originUpstream tracking branchesGitHub accounts, SSH keys and authentication

GitHub · 10 min

Creating and publishing a repository

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by GitHubReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A GitHub repository is a remote copy of your Git history, and publishing one means creating an empty remote and pushing your branch to it.
  • Create the remote empty when you already have a local project, because a README or license added on GitHub gives the remote a commit your local history does not have.
  • git remote add origin <url> names the remote, and git push -u origin main uploads main and sets it to track origin/main.
  • Choosing public or private is a decision about who can read the code, and a repository you push has its whole history visible to that audience.
  • Add a README, a .gitignore and a license early, since they tell visitors what the project is, keep junk out, and say what others may do with the code.

What publishing a repository means

You already have a repository: the project folder with a .git directory, and your commits. It lives on your disk, so if the disk dies the history goes with it, and nobody can collaborate on it. GitHub hosts a second copy that other people can fetch from and push to.

Publishing is therefore two small steps. Create an empty repository on GitHub, then tell your local repository about it and push. Everything you learned about remotes applies. GitHub is just the server at the other end.

Create it empty

On github.com, click the plus icon in the upper-right corner (its label is “Create something new”), then New repository. The form has a Repository name field, an optional description, a visibility choice, and an Add README toggle. On the form I have seen, there are also options for a .gitignore and a license. The quickstart page I read does not mention those two, so check your own screen.

If you already have a local project, leave the README, .gitignore and license unticked. The reason is mechanical. Ticking them makes GitHub create the repository with a first commit. Your local main has a different first commit. The two histories share no ancestor, so the first push is rejected. I reproduced it with a local stand-in remote: git push -u origin main printed ! [rejected] main -> main (fetch first), and a plain git pull then stopped with Need to specify how to reconcile divergent branches. You can fix that with a pull that allows unrelated histories, but it is friction you chose. Start with an empty repository and push yours.

If you are starting from scratch instead, ticking the README is the easy way to get a repository you can git clone straight away.

Public or private. A public repository can be read by anyone, a private one only by people you invite. You can change it later, but the history you pushed is the history that becomes visible. Check what is in your commits before you make something public.

I could not click through any of this, since I have no GitHub account here. The labels come from the GitHub Docs quickstart as I read it on 2026-11-23, and the page layout changes often, so match the descriptions rather than trusting my wording.

Connect and push

GitHub then shows the commands. For the notes CLI they are:

git branch -M main
git remote add origin git@github.com:ada/notes-cli.git
git push -u origin main

I ran the same three commands against a local bare repository standing in for GitHub, starting from a repository whose first branch was master.

  • git branch -M main renamed the branch to main. If you set init.defaultBranch to main earlier, this is a no-op.
  • git remote add origin <url> stored the URL under the name origin. git remote -v then listed it for fetch and push.
  • git push -u origin main printed [new branch] main -> main and branch 'main' set up to track 'origin/main'. Afterwards git status -sb showed ## main...origin/main and git branch -vv showed [origin/main] next to main.

The -u is what sets the upstream, so later you can type git push and git pull with no arguments. If you use SSH, the URL has the git@github.com: form. If you use HTTPS, it starts with https://. You can switch later with git remote set-url origin <new-url>, which I also ran, and git remote get-url origin printed the new address. The two forms and when to choose each are in the previous lesson.

The command-line shortcut is gh repo create, which creates the remote, adds it and optionally pushes in one go. The GitHub CLI lesson covers it. I did not run it, because gh is not installed here.

The three files worth adding first

  • README.md. The page visitors see. Say what the project is, how to install it, and how to run it. For the notes CLI that is three commands: npm test, node src/cli.mjs add "text", node src/cli.mjs list.
  • .gitignore. Keep node_modules/, build output and local data out of history. Do it before the first commit, since files already committed stay tracked. The gitignore lesson explains the rules. The notes project ignores node_modules/, dist/ and notes.json.
  • A license. Without one, the default is that nobody else may legally reuse your code, even if it is public. Common choices are MIT and Apache 2.0, and choosealicense.com compares them. A license is a legal choice. Pick one deliberately, and read what it says.

A mistake that is hard to undo

If you commit a secret, such as an API key, and push, treat the secret as leaked, because any clone has it. Rotate the credential first. Rewriting history comes second and is covered in removing secrets from history. Later in this group, secret scanning catches some of these before they land.

What the video covers

GitHub’s own “How to create your first GitHub repository: A beginner’s guide | Tutorial” runs about nine and a half minutes and was published around 2024. The description says it breaks down the process of creating your first repository. I could not read a transcript or chapters, so I do not know how much of the time goes on the website versus the command line. English is judged from the title and the channel. It is an official video, which makes UI names more likely to be right when it was recorded, but the New repository form has changed since. If the video and your screen disagree, trust the screen.

Where this fits

Your code is on GitHub. The main reason to be there is collaboration, which on GitHub starts with the pull request: pull requests.

Check yourself

  1. 01You have a local project with commits. Why should the new GitHub repository be created empty?

    If GitHub adds a README or license, the remote has a commit that your local branch lacks. Your first push is then rejected until you pull and merge, which is avoidable friction.

  2. 02What does the -u in git push -u origin main do?

    It sets origin/main as the upstream of your local main. After that, plain git push and git pull know where to go, and git status shows how far ahead or behind you are.

  3. 03You committed a password and then pushed to a public repository. Does deleting the file in a new commit fix it?

    No. The password is still in the history that everyone can fetch. You must treat it as leaked and revoke it. Cleaning history is a separate step, covered in the removing secrets lesson.

  4. 04What is the difference between origin and main in git push origin main?

    origin is the name of the remote, a stored URL. main is the branch you are pushing. The command means send my main branch to the remote called origin.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark