Git Basics — An Idiot’s Guide.

We are consistently making changes to files as programmers. You add new lines of code, you pull out old ones or refactor them. With this comes a risk, you could eventually mess things up and want to go back in time to a point where everything worked perfectly. Well you could probably have decided to “ctrl + z ” till whenever that was. But why would you want to kill yourself?

Behold Git. See the light.

Image for post
Image for post

Git is a version control system used for tracking changes made to files. Although largely used by programmers, git can be used by virtually anyone…even your village people (ask Nigerians.), to track changes made to your project.

Git was invented in 2005 by Linus Torvalds. You most likely didn’t come here for history classes. So let’s forge on.

To start using git for version control, git has to be installed on your computer. To install git, follow the link:

Now that we have git installed. In your terminal, navigate to your project folder and run the following code.

git init

The above command initialises the folder as a git repository and tells git to start keeping track of changes made to files in that folder. You can simply just open the project folder in Visual Studio Code and run the command in the terminal. That’s what I will be using.

To add a new file… you can just create it normally…or run the following code in your terminal (for mac users):

touch <fileName>

I have created a sample index.html and index.js file as sample. My folder looks like this:

Image for post
Image for post

We just created those files. To the check the status of our files, if they are being tracked or not, run the following code

git status

If we run the above line of code in our terminal, git tells us about the files that were created and that they are not being tracked.

Image for post
Image for post

Now we have to commit these files to git… so that git can keep track of them.

Before we proceed, let me take a moment to clarify certain things.

In git, there are three levels, these are:

  1. The Git Repository
  2. The Staging Area (or Staging Index)
  3. The Working Directory

The Git Repository:

This is where git stores files that have been committed (handed over) to it to track. Here, the history of changes made are kept and each one has a unique key known as SHA key attached to it, with which you can navigate to that particular version of the file or project.

The Staging Area:

The staging area, is like an intermediary between the git repo and your working directory.

The Working Directory:

Your working directory is your normal folder where you are currently working on.

Every-time you make a change to a file in your working directory, git compares the version it has in the git repo to what you currently have in the working directory and let’s you know that the file has been modified.

Alright let’s go back. Git was telling us we had untracked files.

To make git track these files run the following commands on your terminal

git add <fileName>

Adds the relevant file to the staging area…you can use (git add .) to add all the files.

git commit -m <add a message here relevant to changes you made>

This takes the file from the staging area to the git repo…

Now if we run the git status” command…. We see that there’s nothing to track.

To read and review all the changes that have been made to your git repo, simply run the following code

git log

We’ve only made one commit. So this is what our history looks like.

Image for post
Image for post

Git also allows us create branches. Say you want to try out a new idea, like create a login system on your project and you don’t want to risk messing up with your main project. Rather than copy the entire folder into a new folder, you can just create a separate branch, where you try out your new idea. Once you are done and sure everything is working fine, you can merge this branch into your main branch.

To create a new branch, first ensure all changes made have been committed to your git repo.

git checkout -b <new-branch-name>

To view the branches you have and know the current branch you are on. Run the following code:

git branch

You can switch from one branch to another and work on the different branches simultaneously. The changes made in each branch isolated from the other.

git checkout <the-name-of-the-branch-you-intend-to-switch-to>

Switch to the branch you want to merge into. Then:

git merge <the-branch-you-would-like-to-add-to-the-existing-branch>

Say you no longer want a branch and you want to delete it.

git branch --d <branch-name>

Let’s say you made a mistake and you want to return back to a point in time where everything was working well, do a “git log”, copy a part of “SHA key” of the particular history you want to revert to.

Image for post
Image for post

Then run the following command:

git checkout <SHA key-of-the-commit-you-want>

Git also gives you the chance to upload your files to a remote repository (online location). So, other developers can access it if you want…or just to store it.

While there are several online platforms for storing git projects, github appears to be the most popular. And what I am going to use for this illustration. You need to have an account first, if you don’t, you can visit their site

Step 1:

Create a new repo. In the top right corner, click on the plus (+) icon….then click on new repository

Image for post
Image for post

Step 2:

Enter a name for your repository and description. Then click on create new repository (the green at the bottom left corner)

Image for post
Image for post

Step 3:

Copy the following line and paste your in terminal.

Image for post
Image for post

The command is basically:

git remote add origin <the-link-to-the-repo>

To confirm that the above command work, you can just type in “git remote” in your terminal and you should see “origin”.

Step 4:

Congrats. You just successfully added a remote repo to your project. Now let’s push our project online.

Image for post
Image for post
git push -u origin master

This is for your first push. Subsequently, to keep updating your online repository you just run the command.

git push

There’s more to know about git, but i hope this gives you a basic introduction. Thanks for reading.

Written by

In real life I love to write, tell stories and madly enjoy reading screenplays. I’am Currently Training as a Software Engineer at Decagon Institute

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store