How to push an existing project to GitHub

0 Shares
0
0
0
0

Introduction

GitHub is a cloud-hosted Git management tool. Git is a distributed version control, meaning the entire repository and history is live wherever you put it. People tend to use GitHub in their business or development workflows as a managed hosting solution to back up their repositories. GitHub takes this even further by allowing you to connect with colleagues, friends, organizations, and more.

In this tutorial, you will learn how to take an existing project you are working on and push it to GitHub.

Prerequisites
  • A free GitHub account
  • git is installed on your local machine

Step 1 – Create a new GitHub repository

Log into GitHub and create a new empty repository. You can choose whether to initialize a README or not. This doesn't really matter because we're just going to overwrite everything in this remote repository anyway.

Warning: In the rest of this tutorial, your GitHub username is Sammy and the repository you created is called my-new-project. It's important to replace these places with your actual username and repository name.

Step 2 – Launch Git in the project folder

After navigating to the folder you want to add, run the following commands from your terminal.

Step 3 – Set up the Git Repo

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

Note: If you already have a basic Git repository, you can skip this command.

git init

This step creates a hidden .git directory in your project folder that the git software recognizes and uses to store all the metadata and version history for the project.

Add files to the Git directory
git add -A

The git add command is used to tell git which files to include in a commit, and the -A (or –all) argument means “include all.”.

Commit the added files.
git commit -m 'Added my project'

The git commit command creates a new commit with all the files that were "added". -m (or -message) sets a message that will be included with the commit and used for future reference to understand the commit. In this case, the message is: "I added my project".

Add a new remote source
git remote add origin [email protected]:sammy/my-new-project.git

Note: Remember, you need to replace the highlighted username and repository name parts with your username and repository name.

 

In git, a “remote” refers to a remote version of the same repository, usually located on a server somewhere (in this case, GitHub). “origin” is the default name git gives to a remote server (you can have multiple remotes), so git remote add origin tells git to add the default remote server URL for this repository.

Push to GitHub
git push -u -f origin main

The -u (or --set-upstream) flag sets the remote origin as the upstream reference. This allows you to later do git push and git pull commands without having to specify the origin, as we always want GitHub to do in this case.

The -f (or –force) flag stands for force. It automatically rewrites everything in the remote directory. We use it here to overwrite the default README that GitHub automatically initializes.

Note: If you didn't include the default README when creating the project on GitHub, the -f flag isn't really necessary.

All together
git init
git add -A
git commit -m 'Added my project'
git remote add origin [email protected]:sammy/my-new-project.git
git push -u -f origin main

Result

Now, you are ready to track your code changes remotely on GitHub.

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like