Create and Initialize a New GitHub Repository from the Command Line
Jonathan Bowman Created: August 01, 2020 Updated: July 01, 2023 [Dev] #git #GitHub #commandlineI 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
🔗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:
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
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:
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”:
Then, on the left, select “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
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 [email protected]:USER/NEW_REPO_NAME.git
🔗Push new repository to Github
Using the URL we snagged above, add the remote repository as the origin:
Then rename the master
branch to main
. Easy to do. The right thing to do.
Finally, push the main branch to remote:
Check out your newly populated repo in GitHub, and happy coding!