Sub topic 3
Creating an Empty Repository on GitHub
Prerequisites
This chapter assumes you have Git installed with user.name and user.email set, and that you are comfortable with git add and git commit. You also need a GitHub account and a local repository with at least one commit. The previous chapter explained what a remote is. Here you create the remote's other half: a repository on GitHub that is ready to receive your history.
Why the new repository should be empty
GitHub's creation form offers to add a README, a .gitignore file, and a license. Each option makes GitHub create a first commit in the new repository.
Your local repository already has its own first commit. Two repositories with different first commits share no history. Git treats them as unrelated, and your first push will be rejected. For an existing local project, create the GitHub repository with none of those files.
If you are starting a brand-new project on GitHub and plan to clone it, those options are fine. This tutorial covers cloning in a later chapter.
Check your local starting point
Before you create anything, confirm the local repository is in good shape.
git status
git log --onelinegit status should report a clean working tree. git log --oneline should list at least one commit. If either check fails, fix that first, because there is nothing to push without a commit.
Create the repository in the web interface
- Sign in to GitHub and open
https://github.com/new. You can also click the + menu at the top right and choose New repository. - Under Owner, pick your personal account or an organization you belong to.
- Enter a Repository name. Names can contain letters, numbers, hyphens, underscores, and periods. GitHub converts spaces to hyphens.
- Optionally add a Description. You can edit it later.
- Choose Public or Private.
- Leave Add a README file, Add .gitignore, and Choose a license unset.
- Click Create repository.
| Setting | Recommended value | Reason |
|---|---|---|
| Name | Same as your local folder | Easier to recognize, but not required to match |
| Visibility | Private if unsure | You can make it public later, but anything already public may have been copied |
| README | Off | Avoids an unrelated first commit |
| .gitignore | Off | Add it locally and commit it yourself |
| License | Off | Add it locally when you choose one |
Visibility can be changed later under Settings, in the Danger Zone section.
Read the quick setup page
After creation, GitHub shows a quick setup page for the empty repository. It lists the repository's URL in two forms, HTTPS and SSH, and suggests commands for connecting an existing local repository. The URLs follow this pattern:
- HTTPS:
https://github.com/YOUR-USERNAME/YOUR-REPO.git - SSH:
[email protected]:YOUR-USERNAME/YOUR-REPO.git
Copy one of them for later. The next chapters explain how to choose between them and how to authenticate.
Common mistakes
- Choosing a name that already exists in your account. GitHub reports that the repository exists already and will not create it.
- Creating the repository under an organization when you meant your personal account. The URL then contains the organization name instead of your username.
- Copying the URL for the wrong protocol. Keep it consistent with the authentication method you plan to set up.
Exercise
Why should you leave "Add a README file" unchecked when connecting an existing local repository to a new GitHub repository?
Show answer
A README makes GitHub create its own first commit. That commit shares no history with your local commits, so the first push is rejected, and you would need to merge the two with --allow-unrelated-histories.