Create Remote Git Repo from Commandline

Introduction

Creating a new Git repository is a common task for developers. While you can always do this from the web interface of GitHub or GitLab, using command-line tools can significantly speed up your workflow, especially when you're already working in the terminal. In this post, we'll explore how to use gh for GitHub and glab for GitLab to create a remote repository and push your local project to it.

Using gh to Create a GitHub Repository

The gh command is the official GitHub CLI tool. If you don't have it installed, you can follow the installation instructions on the official website.

Once installed and authenticated, you can create a new repository with the following command:

gh repo create

This command will start an interactive prompt, asking for the repository name, visibility (public, private, or internal), and whether you want to clone it or push an existing local repository.

To create a repository and push your current local folder, you can run:

gh repo create <repo-name> --public --source=. --remote=origin
git push -u origin main

Replace <repo-name> with your desired repository name. The --source=. flag tells gh to use the current directory as the source, and --remote=origin sets the remote name.

Using glab to Create a GitLab Repository

For GitLab users, glab is the official CLI tool. You can find installation instructions on its official documentation.

After installation and authentication, creating a new repository is just as simple.

To create a new repository interactively, run:

glab repo create

This will guide you through the process of naming your repository and setting its visibility.

To create a repository and push your current local project, you can use:

# Official gitlab
glab repo create <your-namespace>/<repo-name> --public
# Private host gitlab
glab repo create <your-private-host>/<your-namespace>/<repo-name> --private
# Add remote and push
git remote add origin "https://<host>/<your-username>/<repo-name>.git"
git push -u origin main

Replace <repo-name> and <your-username> accordingly. glab will create the remote repository, and then you can add it as a remote and push your code.

Conclusion

Using command-line tools like gh and glab can make your development workflow more efficient. With just a few commands, you can initialize a local repository, create a remote one, and push your code without ever leaving the terminal.