Create and initialize a new Github repository from the command line
Tags: git, github
Published: 2020-08-01 13:10:16

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

```bash
git init
```

## Create a .gitignore

Create a file called [`.gitignore`](https://git-scm.com/docs/gitignore) with any files patterns listed that should not be included in the repository.

You can browse GitHub's [gitignore examples](https://github.com/github/gitignore) or generate one at [gitignore.io](https://www.gitignore.io/) if you want a starting point.

For an example and additional notes, feel free to glance at [my `.gitignore` article](https://dev.to/bowmanjd/generating-a-gitignore-file-203m).

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

Create a [`README.md` file](https://www.makeareadme.com/) that describes the project. Something like this is generally appropriate, and can be adapted:

```markdown
# 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)
```

[Choose a license](https://choosealicense.com/) 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

```bash
git add .
```

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:

```bash
git commit -m "feat: Initial commit of project structure, license, and readme."
```

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](http://udacity.github.io/git-styleguide/) [guides](https://www.conventionalcommits.org/) for [git commits](https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project). I particularly like the [one from Udacity](http://udacity.github.io/git-styleguide/). [Tim Pope's guidance](https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html) 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](https://docs.github.com/en/github/authenticating-to-github/creating-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](https://dev-to-uploads.s3.amazonaws.com/i/0r0n9zc6dfdhijoa2j4u.png)

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

> ![Github Developer Settings](https://dev-to-uploads.s3.amazonaws.com/i/pizdf29r04x9089du8cy.png)

> 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

```bash
curl -u USER https://api.github.com/user/repos -d '{"name":"NEW_REPO_NAME","private":false}'
```

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](https://docs.github.com/en/rest/reference/repos) 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:

```bash
git remote add origin CLONE_URL
```

Then rename the `master` branch to `main`. [Easy to do. The right thing to do.](https://dev.to/afrodevgirl/replacing-master-with-main-in-github-2fjf)

```bash
git branch -m master main
```

Finally, push the main branch to remote:

```bash
git push -u origin main
```

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