Using Git Version Control – The Simple Guide

You maybe developing projects every single day, but if you don’t have version control, then, you are missing a main component of development.

Or, you maybe creating projects and storing them on Google Drive or Dropbox or even iCloud so in case something happens, you can get them from there. But, did you know about Github?

So, let’s not waste much time and jump right in.


What is Git?

Straight from the Git website: Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency

Git Installation

We are going to first start on Git on your local machine so I can explain the many basics of Git that you would need to know.

Now, depending on your OS you would need to install Git on your machine so you can use the command in the terminal.

Now, if you are on a Mac, you would want to just get Git from either the Xcode developer tools, or from https://git-scm.com/.

To check whether you have Git installed, run:

git --version

If it says like Command is not found or something related, that means you don’t have Git installed. Otherwise, if you get the version number, we are ready to move on.

Basic Git Commands

Now, so let’s navigate into a project directory, you can create a new directory or move into an existing one.

To navigate directories, you will want to run:

cd 'Folder Name'

# For Example
cd Desktop/
cd Projects/
cd test_project/

Now, inside of the directory, if it is a blank one, just insert a file of any type. Could be HTML, CSS or even a README file.

Initialize Git Repo

To initialize a Git repo inside a directory, run:

git init

Now it will create a folder inside your repository called: “.git”. It is a hidden folder, so if you were to see your hidden files, you would see that folder.

On a Mac, if you press:

cmd + shift + .

And, on Windows, look in your File Explorer options, and look for: Show Hidden Files.

The .git folder is not really that important, but it is just good to know where it is, in case you ever find the need for it.

Staging Your Files/Changes

So, the file you just added to your directory or the files that already exist, you want to stage them, and let Git know that they exist and that you would want to use version control with them.

So, for example, in my directory I have the following files:

  • index.html
  • app.css
  • index.js

So, to stage each of them, you would run:

git add (file name)

# Example
git add index.html
git add app.css
git add index.js

While these commands maybe easy for just our project, imagine a project that you have over 20 files. Each time you make a change and want to stage it, you will have to run the command for each of the files that you made a change on.

Sometimes, this could be a very lengthy, so luckily Git includes an easier command for us.

git add .

This command essentially will add ‘all’ you files to the staging area. You can think of the staging area as like telling the waiter at a restaurant of what you want. You could call the waiter every-time for each item, or tell the waiter all at once.

Committing Your Staging

The next step is to commit your staging that you have done. Think of this like you paying for the food that you have ordered. This is a commitment. It would not be very nice to change your order to something else after you have ate it.

Now one thing that you will have to learn is known as Writing Commit Messages. Commit messages are essentially like leaving a review at the restaurant. But commit messages have to short and concise.

For now, we will use “Initial Commit” since this is our first commit we are making to the repository.

To make a commit, run:

git commit -m "Initial Commit"

You could replace Initial Commit with anything, it is totally dependent on you, but you really need make sure that it is short and concise.

Putting Together, What we have Learned So Far.

Alright, so it has been alot of reading so far, but we have learned so much, that I want to put it together in one small gist, so you can always remember it.

The Gist

  1. Initialize Repository: git init – in the directory
  2. Stage Your Files/Changes: git add (filename) or .
  3. Commit Your Stage: git commit -m “Commit Message”

So in just three steps, you are able to get version control on your git repo running.

Now, let’s push this code to GitHub so you can have a backup of what you are creating.

Github

First, head over to GitHub.com. You will want to create an account, and don’t worry it is FREE.

Then, you would want to create a New Repository. It will ask you the following:

Let’s go through these fields.

Your Repository Name should be relating to the project you are working on. For example, if you are creating an application, you might want to create the repository of the name of the application.

Also, make sure to keep your repository name short and concise so it is easy to read/understand.

Then, it asks you a description, which is exactly how it sounds.

Now, comes the part of public vs private. Depending on your project, you will want to make this choice. If you want other people to be able to discover your project, then you might chose Public.

However, if you want you only to look at the project, then chose private. At the end, it is ultimately your choice.

At last, it will ask you whether you want to initialize your repository with a README.md file or not. Go ahead and not check the option right now.

After clicking Create, you will be brought to this page.

So, we want to look under the second section where it talks about an existing repo.

The nice thing about GitHub is that it gives you exactly what you have to run in the terminal.

Inside your terminal, paste the add origin command. Then, paste the push command.

After the push command, it will ask you to login to GitHub, so login with the account you created the repository with.

Finally, your repository is now pushed to GitHub and is officially now has a backup.

Doing It Again.

So, you may be creating new files and changing files will continue in your project, so how do you push your code again. You won’t create a new repository for each change, that would be silly.

Instead, you will be running some of the commands from now on, for that repository.

git add . # for all changes/files
git commit -m "Changed Files" # for committing your changes
git push # to push your changes to github

With just three lines of code, you project will have a backup and you will have access to version control.

During this whole entire article, I was talking about Version Control. And version control allows you to go back in time of your project. Maybe, you came across an error, you might want to go back to a working state.

So, in your terminal, you can run:

git log

This will allow you to see all your previous commits that you have made.

To go back to an older commit, run:

git checkout <commit hash>

So an example would be:

git checkout 1bcad1d866d71a6ffb3a19244fdb5167877526e9

And you will receive a message saying that you are now in this specific time in your project.

Conclusion

Congratulations! You have successfully learned the basic git commands that you need to get started with git.

There are many other commands, and I will be sure to do another post about those at a later date. In the meantime, you could checkout this video, if you want more information about Git itself and the more advanced commands that you could run.

Alright, so have a great time developing! Welcome to 2020.

Leave a comment

Design a site like this with WordPress.com
Get started