How to Push Existing Repo to GitHub
Tags: tutorial, github, git, beginners
Published: 2022-08-09 20:40:00

### Introduction

In this tutorial, you will learn how to push your locally hosted project code to GitHub.

### Prerequisites

To initialize your local repository and push it to GitHub, you will need:

1. [A GitHub account](https://github.com/)
2. [Have `git` installed on your local machine](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)

### Create a new GitHub repo

Sign into GitHub and create a new empty repo. 

> ![how to create a new repository page on GitHub](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o6klit2eqw0zrtqwia51.png) 

### Git Commands

Run the following commands after navigating to the project folder you want to add and push to GitHub.

#### Initialize the Git repo

Make sure you are in the root directory of the project you want to push to GitHub and run: 


```
git init -b main
```

This step creates a hidden `.git` directory in your local project folder used to store all version history and metadata for the project.

#### Add and commit files

```
git add . && git commit -m "initial commit"
```

The `git add` command tells git which files to include in the commit. The `-A` or `--all` argument means "include all" files in that project folder.

The `git commit` command creates a new commit with all the files that have been added from the previous step. The `-m` or `--message` sets the commit message that will be included with the commit.


#### Check status

```
git status
```

The `git status` command displays what changes have been staged. In this case, you want to check that all files from your local project directory have been added. 
 

#### Add new remote repo

```
git remote add origin <GitHub repo link>
# sets the new remote

git remote -v
# verifies the new remote URL
```

In git, "remote" refers to a remote version of the same repository, typically hosted on GitHub. "origin" is the default name git gives to a remote repo. `git remote add origin` is telling git to add the URL of the default remote server for this repo.

> You can run the `git remote -v` command to check that your GitHub repo was added as the remote repo that you will be pushing your local changes to. 

#### Push to GitHub

```
git push -u -f origin main
```

The `-u` flag sets the remote `origin` as the upstream reference that allows you to perform `git push` and `git pull` commands without specifying an `origin`.

The `-f` flag stands for _force_ and will automatically overwrite everything in the remote directory. 

### All steps

```
1. git init
2. git add . && git commit -m "initial commit"
3. git remote add origin <GitHub repo link>
4. git push -u -f origin main
```

### Conclusion

Now you are all set to track your local code changes remotely in GitHub and understand the basics on how to add project files, commit changes, and push your code. The steps listed above are best for personal projects.

Follow steps 2 and 4 to push your local code changes to your GitHub a couple of times an hour to avoid large commits.

### Where to Find Me

Feel free to reach out to me with any questions on [Twitter](https://twitter.com/theekrystallee) and follow me on [TikTok](https://www.tiktok.com/@theekrystallee).

Happy building 🧱