Basics of Git and GitHub

Introduction

Git is a free and open source version control system for tracking changes in computer files. Version control system is the way in which programmers track their code changes. While GitHub is the website where you host all your git repositories (repo). By hosting your git repo online it makes it easy to work in a group with other people and organize your project into a portfolio for you to show potential employers.

To use git you need to have these on your computer

  • The first thing you need to do is to sign-up for an account on GitHub
  • You need to have git install on your computer, to check if you have git on your computer, simply open your terminal and run

git –version

If nothing is shown, then you can install git by following this tutorial.

  • The next thing you need to have is a code editor, although you can write code on any type of word processing software, but I recommend using Visual Studio Code by Microsoft which has an inbuilt terminal attached. The code editor will be used for making changes locally on your machine.

How to create a git repository.

Let’s say you have a folder with three files (index.html, style.css, and app.js) , to create a git repo you need to switch into the project folder and run the following commands.

git init

This command creates a .git folder in the project directory. The folder is hidden by default.

git add "file name"

This command move the specified file to the staging area. To move all files in the directory to the staging area you can simply use the command

git add .

The period stands for all

  • From the previous steps, the files are in the staging area and ready for commit. To commit files, you use the command

Git commit –m “This is the commit message”

You can change the text inside the double quotation to the commit message that you want to use. Let’s say you make changes to the project, you need to repeat step 2 and 3 to commit the changes.

How to send git repo to GitHub website.

  • Log into your GitHub account, at the top right corner there is a plus sign and an arrow pointing to the bottom, click on the arrow and select “new repository”.
  • Write the repository name in the box provided, you may choose to add a README file, after the whole selection click on the “create repository” button.
  • You will be displayed with all the steps for uploading a git repo to the GitHub website. Copy the URL that is given at the top which looks like github.com/username/repo-name.git
  • then go to your terminal where you create the git commit and run the command

git remote add origin "the URL you copied"

  • then you run

    git push –u origin main

this will push the repo to the GitHub website, at this point you will be asked to enter your GitHub login details.

After these steps if you refresh the project homepage on GitHub website, you will see your project live.

To download code from GitHub website to your local machine, simply click on code in the project homepage on GitHub website and copy the URL that is given at the drop down, then go to your terminal and run

Git clone "the URL you copied"

After making changes to an existing git repo on your local machine you can send the changes back to GitHub website by using

Git add .

Which adds all the changes to the staging area.

Git commit –m “this is an update on the previous repo”

This command commits the changes.

Git push –u origin main

This command pushes the changes back to GitHub.

Summary

  • git is a tool that track your changes over time.
  • GitHub is a website where you host your git repo.

Some of the git commands are:


Git init                                    //initialize local git repo.

Git add <file>                           //add file(S) to index.

Git status                                //check the status of a working tree.

Git commit                              //commit changes in index.

Git clone                                 //clone repository into a new directory.

Git pull                                    //pull latest from remote repo.

Git push                                  //push code to remote repo..