DEV Community

Chandler for Casual Coders

Posted on

How To Create and Push a New Git Repo to GitHub

To create a new Git repository locally and commit all files, you will need to have Git installed on your computer. If you don't have Git installed, you can download it from the official website.

Once you have Git installed, follow these steps to create a new repository and commit all files:

Open a terminal or command prompt and navigate to the directory where you want to create the Git repository.

Run the following command to initialize the repository:

$ git init
Enter fullscreen mode Exit fullscreen mode

This will create a new .git directory in the current directory, which will be used to store all the version control information for your project.

If you don't have any files to commit, make an example file called README.md and add The title of your project preceded by a hashtag (or pound symbol if you prefer the classical term) along with a short description of your project a few lines below. For example:

# This is a markdown title

This is a markdown paragraph.
Enter fullscreen mode Exit fullscreen mode

Once you have some files, you can add them to the repo.

Add all the files you want to include in the repository by running the following command:

$ git add .
Enter fullscreen mode Exit fullscreen mode

This will add all the files in the current directory and its subdirectories to the repository. You can also specify individual files to be added by replacing the . with the file names, separated by spaces.

Once all the files are added, you can commit them to the repository by running the following command:

$ git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode

This will create the first commit in the repository, which will include all the files that were added in the previous step. The -m flag is used to specify a commit message, which should briefly describe the changes that were made in this commit.

To create a new empty Git repository on Github, follow these steps:

  1. Log in to your Github account and go to the Github homepage.

  2. Click on the plus icon in the top right corner of the page, and select "New repository" from the dropdown menu.

New Repo Button

  1. On the next page, enter a name for your repository in the "Repository name" field. This should be a short and descriptive name that accurately reflects the contents of the repository.

  2. In the "Description" field, you can enter a longer description of the repository if you want. This is optional, but it can be helpful to provide more information about the purpose of the repository.

  3. You can set the visibility to whatever you prefer. If you want others to be able to see your work, set it to "Public". Otherwise, set it to "Private".

New Repo Page

  1. Leave the "Initialize this repository with a README" option unchecked, as we want to create an empty repository.

  2. Click on the "Create repository" button to create the repository.

Empty Repo Page

Your new empty repository will be created and you will be taken to the repository page. From there you can copy the project URL (usually ending in .git) and push your local changes to it.

Clone URL

Run the following command to add the remote, replacing <github_project_url> with the URL of the Github project you copied:

$ git remote add origin <github_project_url>
Enter fullscreen mode Exit fullscreen mode

This will add the remote to your local repository. You can verify that the remote was added successfully by running the following command:

$ git remote -v
Enter fullscreen mode Exit fullscreen mode

This will list all the configured remotes for the repository, along with their URLs. The remote you added should be included in the list.

Once the remote is added, you can push your local commits to it by running the following command:

$ git push origin main
Enter fullscreen mode Exit fullscreen mode

Git should prompt you to log in. Enter your GitHub username and password in the prompt window.

This will push all the local commits on the main branch to the remote repository.

If you add any additional files, you will need to run git add . again. After you make any additional changes, simply repeat the steps of git commit and git push, but be sure to change the message (the part within the quotations) to reflect what you did. Messages should be short and concise, with a general rule of thumb to be to keep them less than 50 characters. For example, "Add new function x" would be acceptable.

Was this helpful? Let us know in the comments below!

Top comments (0)