How to Push a New Repository to GitHub from the Command Line

How to Push a New Repository to GitHub from the Command Line

Prerequisite

  • Have a GitHub account
  • Basic understanding of Linux command line

Introduction

GitHub is a platform that allows us to host our code for free. It also allows others to work on our code with us (collaboration) and lets us update your code from your local machine. Git is an open-source project that runs as a software. It is used to track changes to our code and allows us to revert to previous versions of our code if the need arises.

Screenshot from 2022-11-11 18-20-02.png

GitHub can be described as a store, a place that contains everything you need, in this context your code while Git is the car that drives you to the store.

We have built a calculator application for #100daysOfCode; we are excited to show this to the world. How do we move the code from our local machine to GitHub?

In this scenario, we will upload our files to GitHub using git. The folder our file is contained in is called a repository.

A repository on GitHub holds information about everything that has been done on the files contained in it, as long as those changes have been tracked by git.

This means in the near future, if we want to update our calculator application, for instance, add some cool CSS animation to it. Our repository will have information about our code before the CSS animation and after. We can revert to any version when we please

Pushing Code to a New Repository on GitHub

We have our code running on our system and we need to push it to GitHub. Create a new repository on GitHub, How to create a repository on GitHub

  • Open up the terminal and navigate to the location of our project.
  • In the terminal:

    git init
    

    This command is typically the first command in a new project. It initializes a new Git repository, this means it tells git this is a repository and it should be treated as such. This gives git context when we run other commands.

  • Navigate to the new repository you created on GitHub and copy the https url to the repository

  • On you terminal:
    git remote add origin <url>
    

This command points git to the GitHub repository it will be working with. It says, “hey git, this remote repository is my origin, all code and changes will be pushed to or pulled from this repository”.

We have pointed git to the repository we want it to make changes to, now to make the changes.

We have to identify ourselves to git so it knows who is making the changes.

  • In the terminal:

    git config --global user.email "youremail@example.com"
    
      git config --global user.name "Your Name"
    

This gives git information about our name and our email address

  • Now to add the changes, In the command line, type:
    git add .
    

This command tells git, whatever changes I have made to my files up to this point, I want you to save them. When I am ready to push my code to GitHub, these will be the changes that will be pushed. This is called staging.

  • Commit our changes
    git commit -m ‘My first commit’
    

This command tells git, take the files I have in my staging area and save them. This means the changes we have made are now saved in our local repository. The flag -m means message, the message is My first commit

  • Push the changes to GitHub
    git push <https url>
    

This command tells git to push the changes we have saved on our local repository to GitHub.

  • Before pushing the changes, git will ask for our username and password.
    • Your username is your GitHub username
    • Your password is your GitHub access token.

If you have never created an access token before, Check out this article to create one and get started

Navigating back to our github repository, we will see the files we added and the message.

first.png

Congratulations! We have pushed our code to GitHubusing git.

Adding new changes to our repository

New changes we add to our file will be done in a shorter process.

git add . 

git commit -m ‘my second commit’

git push

This is because we have already told git to treat this folder as a repository and pointed it to the folder in GitHub to push to.

Conclusion

Git and GitHub work together to help developers host their code, update the code and track changes to the code. Git is an open-source project that is used for version control and GitHub is a platform that is used to host code.

Resources