Creating a remote repository from the command-line

November 30, 2024

When creating a new project, I often find myself going through the process of visiting GitHub to manually create a new repository, cloning it on my local machine and then finally moving all of my files within that new directory.

Well, no more!

Here are two methods to create a remote repository entirely from the command-line.

Before you do anything though, make sure to have the following two programs installed on your machine:

Method 1: Creating a remote repository with the GitHub CLI

This method is perhaps the “simpler” of the two, but it requires a bit more ahead-of-time thinking.

  1. Create a remote repository using the gh repo create command.

    gh repo create --public [repo-name]
    
    • --public or --private: Sets the repository’s visibility.
  2. Clone your new remote repository to your machine.

    git clone [email protected]:[username]/[repo-name].git
    
  3. You now have a fully-connected repository for your project on your machine. Go in there, and do your work!

    cd [repo-name]
    

Method 2: Creating a remote repository from the current directory

This method is my preferred one, given that I tend to start working locally before I even set up a remote repository.

  1. Initialize a Git repository within a local directory.

    cd [repo-name]
    git init
    
  2. Create a remote repository using the gh repo create command, and by passing the current local repository as the source.

    gh repo create [repo-name] --public --source=. --remote=origin
    
    • --public or --private: Sets the repository’s visibility.
    • --source: Specifies which local repository should be used as the “source” for your new remote directory.
    • --remote: Specifies the name for the remote (conventionally named origin).
  3. You now have a fully-connected repository for your project on your machine. Go in there, and do your work!

Conclusion

Either method works equally well. Just pick the one that applies best to your current scenario!

Tags

command-line
terminal