GitHub(Git management tool) has become an essential platform for version control and collaborative development making it possible for developers to work together seamlessly.
Before you can utilize the goodness that comes with GitHub, you first need to push your project to the platform. In this guide, you will learn how to push a new project to GitHub.
Let's get started.
Make sure you have the following:
- A GitHub account
- Git installed on your local machine. Follow this guide to install Git.
- A project that is not on GitHub yet.
Step 1: Ensure git is tracking your project locally
Using the terminal, navigate to the project you want to push to GitHub using the cd command. My project is on the desktop. So this is the command I run to get to it.
Once you are in the right directory, run the command below to initialize a Git repository
This command creates a hidden
.git folder that stores the version history of your project.
Stage and Commit Your Changes
Once git is initialized, you can add your untracked files to the staging area using the command below.
git add .
The next step is to commit your files to a new save point using the command below.
git commit -m "Initial commit"
Your files are now ready to push to the remote repository. Let's now prepare the remote repo.
Step 2: Create a remote repository on GitHub
Sign in to GitHub and on your homepage, click on the New icon.
This will take you to a page where you can create your repository.
Give your repo a name, a description, and any other relevant detail. Choose whether you want the repository to be public or private. When done, click on the
Create repository button.
With the repo created, GitHub will give you a link to the repository and instructions on how to link the local repo to the remote one. If you find it confusing, keep reading.
Copy this link as we will use it to link the two repositories together. The local repository will use this URL to push it's contents to the remote repository
Step 3: Link local repository to the remote repository.
Open your terminal and use this command to rename the branch. The
-M flag is used to forcefully move/rename a branch. This step is optional.
git branch -M main
With the repository URL copied, you can link the two repositories together using the command below.
git remote add origin <Repo URL>
Make sure to replace
Finally, push the contents of the local repo to the remote repo with the command below.
git push -u -f origin main
-u flag is used to set the remote origin as the upstream reference. This way, we can perform
git push and
git pull commands without specifying
origin. On the other hand, the
-f flag is to forcefully overwrite everything in the remote repository.
Once you are done, refresh your GitHub and you should see your files in your remote repo.
By following this tutorial, you will successfully add your first project to GitHub. If you found the article helpful, you will find our other tutorials about GitHub useful.
Thank you for reading : )