Sub topic 10
Doing it in one step with gh repo create
The earlier chapters created an empty repository in the web interface, added it with git remote add, and pushed with git push -u. The GitHub CLI (gh) can do all of that in one command. This chapter shows how that command works and where it fails.
This chapter assumes you have these:
- A local repository with at least one commit.
- A GitHub account.
- The GitHub CLI installed from cli.github.com.
What the command replaces
Done by hand, connecting a local repository to GitHub takes four steps. You create the repository in the browser, copy its URL, run git remote add, and push. gh repo create calls the GitHub API to create the repository, then runs the matching Git commands for you.
Sign in first
The CLI needs its own credentials. Run the login once, then run the create command from inside your project directory.
gh auth login
gh repo create my-project --private --source=. --remote=origin --pushgh auth login asks which host to use and whether Git operations should use HTTPS or SSH. It then signs you in through the browser or with a token. Your answer about the protocol decides the URL format of the remote that gh repo create adds. This is the same choice you made in the chapter on HTTPS and SSH URLs.
You can check the login at any time with gh auth status.
What each flag does
| Flag | Effect |
|---|---|
| `my-project` | Name of the new repository on GitHub. |
| `--private` | Makes the repository visible only to you and people you invite. The alternatives are `--public` and `--internal`. The last one applies to organizations on GitHub Enterprise. |
| `--source=.` | Uses the existing local repository in the current directory instead of making a new one. |
| `--remote=origin` | Names the remote that gets added. |
| `--push` | Pushes the current branch's commits after creation. |
Without --source, the command creates a repository on GitHub and does not look at your local files. If you leave out the name and the visibility flag, gh asks you questions interactively.
Check the result
Confirm the setup with the same commands you used for a manual connection:
git remote -v
git statusgit remote -v should list origin with a URL that matches the protocol you chose at login. git status should say your branch is up to date with origin/main, which means the push also recorded the upstream branch.
Common errors
remote origin already exists: the local repository already has a remote namedorigin. Pass a different name with--remote=github, or remove the old remote first withgit remote remove origin.Name already exists on this account: you already have a repository with that name on GitHub. Choose another name.- A failed push after the repository was created:
--pushneeds at least one commit. In this case the repository exists on GitHub but is empty. Commit your work, then rungit push -u origin main. Do not rungh repo createagain. - Not logged in: run
gh auth loginand retry.
When to prefer the manual route
The one-step command suits new projects that you own. Use the manual steps when the repository already exists on GitHub, for example one created by a teammate or inside an organization with special settings. The manual steps also make sense when you want to understand each stage of the process.
Exercise
You run gh repo create demo --private --source=. --remote=origin --push in a directory whose repository has no commits yet. What happens, and how do you finish the setup?
Show answer
The repository is created on GitHub and the origin remote is added, but the push fails because there is nothing to push. Make a commit with git add . and git commit -m "Initial commit", then run git push -u origin main, using your actual branch name.