Cover image for Create and initialize a new Github repository from the command line

Create and initialize a new Github repository from the command line

bowmanjd profile image Jonathan Bowman ・3 min read

I prefer to stay on the command line when creating a new repo, rather than going back and forth between Github's web interface and the command line. Here are the steps I use when creating a new project.

Initialize local git repository

Create the project directory and ensure it is the current working directory, then

git init
Enter fullscreen mode Exit fullscreen mode

Create a .gitignore

Create a file called .gitignore with any files patterns listed that should not be included in the repository.

You can browse GitHub's gitignore examples or generate one at gitignore.io if you want a starting point.

For an example and additional notes, feel free to glance at my .gitignore article.

Recommended: create README.md and LICENSE.txt files.

Create a README.md file that describes the project. Something like this is generally appropriate, and can be adapted:

# PyGreet

PyGreet is a command-line tool that says hello to designated
recipients. It is written in Python.

## Installation

Place `greet.py` in the current working directory.

## Usage

`python greet.py`

## Contributing

Please open an issue to suggest greetings other than "Hello"
or recipients other than "World".

## License

[Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0)
Enter fullscreen mode Exit fullscreen mode

Choose a license that is appropriate for your project and create a LICENSE.txt file, pasting the license text into it.

Add files to repository and commit

After running git status, does the list of untracked files look right? If not, add/edit/remove as you like, and tweak your .gitignore file. Once you are ready, add everything at once with

git add .
Enter fullscreen mode Exit fullscreen mode

Of course, run git status again. (This command should become a nervous tick. Run it when you are bored, confused, or sleeping.) If all is well, run the first commit:

git commit -m "feat: Initial commit of project structure, license, and readme."
Enter fullscreen mode Exit fullscreen mode

The "feat" keyword indicates that the type of commit is a new feature, as opposed to a "fix", "docs", "test", "refactor", etc.

Yeah, there are style guides for git commits. I particularly like the one from Udacity. Tim Pope's guidance from 2008 is still relevant, as well.

Sidebar: set up Github personal access token for authentication

To make GitHub API calls, authentication is necessary. To securely and easily authenticate, create a personal access token in GitHub Settings. First, make sure you are logged in and then click on your profile picture, selecting "Settings":

GitHub Settings

Then, on the left, select "Developer Settings":

Github Developer Settings

Select "Personal Access Tokens" and "Generate new token", labeling it accordingly. For my use, I just need "repo" permissions.

Copy that personal token, because you will not see it again. Place it in your password manager or other encrypted location.

Create a new public GitHub repository

curl -u USER https://api.github.com/user/repos -d '{"name":"NEW_REPO_NAME","private":"false"}'
Enter fullscreen mode Exit fullscreen mode

Substitute your GitHub username (USER) and your desired repository name (NEW_REPO_NAME) in the appropriate places above. Use your GitHub personal access token as the password when prompted. You should receive back a pile of JSON.

Wanted a private repo instead? You know what to do. See the GitHub API docs for more info.

Find the "clone_url" or the "ssh_url" (preferred if you are set up to use SSH with GitHub) from that JSON blob, and copy out the URL value. It should look something like https://github.com/USER/NEW_REPO_NAME.git or git@github.com:USER/NEW_REPO_NAME.git

Push new repository to Github

Using the URL we snagged above, add the remote repository as the origin:

git remote add origin CLONE_URL
Enter fullscreen mode Exit fullscreen mode

Then rename the master branch to main. Easy to do. The right thing to do.

git branch -m master main
Enter fullscreen mode Exit fullscreen mode

Finally, push the main branch to remote:

git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Check out your newly populated repo in GitHub, and happy coding!


Editor guide