How To Create and Push a New Git Repo to GitHub
Tags: github, git, beginners, tutorial
Published: 2022-12-08 13:00:00

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](https://git-scm.com/).

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
```

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:

```md
# This is a markdown title

This is a markdown paragraph.
```

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 .
```

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"
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/24jx7wdvtug0hhue4sq3.png)

3. 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.

4. 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.

5. 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xg3hw4rgzeuwy5bhm9rk.png)

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

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

![Empty Repo Page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wacs02vdct428oiyv7s.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vcmsuovx7pab4b6tv0gu.png)

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>
```

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
```

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
```

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!