Git and GitHub - The Complete Guides - Chapter 1: Git Started
Tags: github, git, webdev, beginners
Published: 2021-08-29 20:43:12

![Greetings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wawlk9o3wj5n9y1yszrr.gif)

If you make websites, mobiles app or write any type of code, it is a good idea to use a <u>version control system.</u>
The most popular and widely used version control system is <strong>GitHub</strong>

Nowadays any professional developer or designer needs to know how to work with Git and Github.

<h2>Git</h2>
Git enables you to record different versions of your project and it allows you to go back in time to check the previous states of your project.

![Git](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vmbowsfhacp70bg3gyo.gif)

<h2>GitHub</h2>
Github is an online service that allows you to share your code with the world and also allows you to collaborate with different developers.

![Git-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzdbcfw21iqc8mzupsk3.gif)

<h3>Chapter 1: Git Basics</h3>
<ul>
<li>What is Git?</li>
<li>Git Setup</li>
<li>Basics of Terminal</li>
<li>Create first Repository</li>
<li>Make first Commit</li>
<li>Check History</li>
</ul>

<h2>What is Git?</h2>
Git is a distributed version control system. Those of you not familiar with Git are probably feeling this explanation is not quite clear.

![huh](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1e7q5rx52st1wd4b4ow0.gif)

To make it a bit simpler, <strong>version control</strong> or you may also hear someone say <strong>source control</strong> is a system that records changes of your file/project and then you are able to recall any specific version of that file/project later.

In other words you can go back in time and get the previous state of your project and even compare it with its current state.

For those of you familiar with gaming, the concept of version control is similar to a <strong>save file</strong>. You would usually save a game after you accomplished a task, since you prefer not to repeat it, in case you fail as you progress further. What's also useful it allows you to go back to that save file whenever you want and play the game exactly how you left it when you saved. <strong><em>That's the basic idea behind version control.</em></strong>

To really see how useful Git is, you have to think how you would go about doing this sort of thing manually. So let's say you have been working on a website and you've completed about half of what you need to do. You are happy so far, so you save the changes you made. You can call this folder version 1 of the website.

Let's say the following day you want to try out something new in the website, that involves changing everything or some part of what you were doing the previous day. However you still want to keep your previous work just in case things don't work out. To make things easy, you will make a copy of the version 1 folder and call the copy version 2 and so on and so on...

![Why-Git](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wtkfvp5ymcl8catvb27i.gif)

Not only is this tedious, it's painful to manage and it can also fill up the space on your storage, especially if you working on really big projects. So this is not the most efficient way of doing this type of thing. This is why the version control system was created.

Version control allows you to make <em>snapshots</em> of the current state of your project which means it saves all the different versions of the project in just one directory.

There are many version control systems out there but the most popular is Git.

![Git-on-top](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nuishow75uy4x9getqui.png)

One of the advantages of Git is <strong>Collaboration</strong>, this is the ability to have different developers work on the same project. The most common way to do this is through Github which is an online service, where you can host your projects. Github is a shareable place where everyone can work on and see your projects. Github will be discussed in later chapters...

We said that Git is a a distributed version control system. In general there are two types of version control systems. <strong>Centralised</strong> system and <strong>Distributed</strong> or <strong>Decentralised</strong> system.
![types-of-version-controls](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rzgdre9ynflgjboy1r3j.png)

Centralised version control systems have a a single server that contains all the different versions of projects so that developers can collaborate. This type of version control has its downsides.

<h2>Drawbacks</h2>
<ul>
<li>Without a network connection then you will not have access to versions</li>
<li>If the server corrupts, you will lose all versions</li>
</ul>

<hr>

Distributed version control systems allows you to save versions of your project locally on different computers and also on the server, each copy of the project is a complete backup of all your data.

<h2>Upside</h2>
<ul>
<li>If the server corrupts, you still have complete project data stored locally.</li>
<li>You don't need network connection in order to save versions of your project.</li>
</ul>

<h2>How Git works</h2>

In order to use Git in your project, first of all you need to <strong>initialise</strong> it. Lets say we have a folder called My-Website in which we have a couple a different files for our website.

After <strong>Git initialisation</strong>, there will be a folder created called <strong>.git</strong>, how to perform Git initialisation will be discussed later...

Once the .git folder is created, that entire directory where the .git folder is placed will become a repository. Sometimes just called a <strong>repo</strong>. A repo is a <em><u>container</u></em> for a project that you want to track with Git.

Now we are able to track changes to files in the entire directory. All the changes will be recognised by Git.

<blockquote>Any changes made outside/above of the directory where the .git folder is placed will not be tracked.</blockquote> 

Now lets say we have a new file we want add to our repo, for instance a javascript file called <em>script.js</em> and we want to set this version of our project in history. We can do that by using <strong>commit</strong> which actually allows us to take a snapshot and set the current state of our project in history.

![repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sj2g4dcmfpls2hfdcj9i.gif)

Here's another diagram to better explain this concept. Let's say the line represents the history or <em>timeline</em> of the development of the project. Before we have started with anything we are at the <em>start</em> of the project. When we start doing some work and we want to set the state of the project we have to do a commit. After some more work we add more files to the project and do another commit to set the state in history.

We have now created the history of the development of the project. Now anytime in the future we can go back in time and get any previous state of the project. In reality we can make as many commits as we want.
![Timeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rkcrd7gonv575gp2umni.gif)
<blockquote>The line in the diagram is called the <strong>branch</strong> or to be more precise it's called the <strong>Master Branch</strong></blockquote>

<h2>Git Setup</h2>
To get properly setup with everything to follow along with the guides we are going to need a <strong>terminal</strong> and a <strong>text editor</strong>.

My recommended text editor is Visual Studio Code, which you can find and download here <a href="https://visualstudio.microsoft.com/" target="_blank">VS_Code</a>
It is relatively simple to download and install.

To find and download Git you can go to https://git-scm.com/
![git-upload](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hk1t4tx0yaebl7tnne3x.gif)
![git-upload-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fy206tyxiht8e9h52byg.gif)
We can see the version of Git by typing the following into the GitBash terminal and pressing enter/carriage return: <strong>git --version</strong>
![git-bash](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/94tnpmwz6m262j6p8a6t.gif)

<h2>Basics of Terminal</h2>
When you open GitBash, by default the current directory is the folder of the user. 

To check this you can use the command: <strong>pwd</strong>

To navigate between directories, we can use the command: <strong>cd</strong>

For example to navigate from the user directory to the Desktop. You can enter the command: <strong>cd Desktop</strong>

In order to move back up the directory you can use the command: <strong>cd ..</strong>

You can move up more than one directory by using: <strong>cd ../..</strong>

Now that we are in the Desktop directory, in order to create a folder we use the command: <strong>mkdir</strong>
We use this command followed by the name of the folder you want to create. for example: <strong>mkdir myFolder</strong>

You can navigate inside the folder using command: <strong>cd myFolder</strong>

<blockquote>With GitBash you don't have to write the entire folder name, "myFolder". You can enter the starting characters and press <strong>TAB</strong> and GitBash will autocomplete it automatically.</blockquote>

To create files we use the command: <strong>touch</strong>

For example we can create a file in the myFolder directory by typing: <strong>touch index.html</strong>

You can see which files exist in a directory by using the command: <strong>ls</strong>

To rename a file we use the command: <strong>mv</strong>
The <strong>mv</strong> command takes the original name followed by the new name.
For example: <strong>mv index.html about.html</strong>

To copy a file we can use the command: <strong>cp</strong>
The <strong>cp</strong> command takes the name of the file you want to copy followed by the directory you want to place it in.
For example: <strong>cp index.html ..</strong>
This will place the file in the Desktop directory.

<blockquote>Just like with any file system, you cannot place a file with the same name as an existing file in that folder</blockquote>

To delete a file we can use the command: <strong>rm</strong>
This command is followed by the name of the file you want to delete.
For example: <strong>rm about.html</strong>

To delete a folder we can use the command: <strong>rm -rf</strong>
This command is followed by the name of the folder you want to delete.
For example: <strong>rm -rf myFolder/<strong>

To clear the contents of the terminal use the command: <strong>clear</strong>

<ul>
<li>pwd : shows you the current directory to are inside of</li>
<li>cd : To navigate between directories</li>
<li>mkdir : create a folder</li>
<li>touch : To create files</li>
<li>ls : You can see which files exist in a directory</li>
<li>mv : To rename a file</li>
<li>cp : To copy a file</li>
<li>rm : To delete a file</li>
<li>rm -rf : To delete a folder</li>
<li>clear - to clear the terminal</li>
</ul>

<h2>Create first Repository</h2>
To create our first repository we first need to open GitBash and create a folder named gitProject inside our Desktop directory.

To open Visual Studio Code along with the folder we created, use the command: <strong>code gitProject</strong>

Visual Studio Code will now open inside the gitProject directory. Now we need to create a file called index.html inside our gitProject directory.

We can also create a file by using VS-Code (Visual Studio Code)

Like we said before, the first thing we need to do in order to create a git repository is to perform a <strong>git initialisation</strong> 

We do this by typing our first Git command: <strong>git init</strong>

![Git repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ko3g5uiobsh770zt2ss.gif)

As you can see when we perform git initialisation, a new folder called .git is created inside our project directory. If you notice our files inside our directory also changed <em><u>green</u></em>

If you don't see the .git folder, it may be that visual studio code is not set to display it. To change that you can go to the setting in VS-Code. If you still don't see it after making the change, restart the text editor.

![git-repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ljsda8nwy4csqr7tujdt.gif)

So far we have a new folder called .git and our files have turned green but our files are not setup to be tracked by git. This can be proven by the <strong>U</strong> on the right side of our files. This means our files are untracked. We can check whether this is true by using another git command we will be using frequently: <strong>git status</strong>

![git-status](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/18dl9b9ktq1xqxk8hj00.gif)

There are still many git commands we will be discussing later but for any specific commands we do not cover here can be explored by using the <strong>--help</strong> parameter after a git command.
For example: <strong>git init --help</strong>

![git-help](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zqiahn356fcock0a1kvr.gif)

<h2>Make first Commit</h2>
Time to go through the whole cycle, meaning from untracked files until we commit. There are several phases that a file/project goes through but remember in general there are two primary phases, <strong>UNTRACKED</strong> and <strong>TRACKED</strong>.

<ul>
<li>Tracked: files which were in the last snapshot.</li>
<li>Untracked: Files which were not in the last snapshot.</li>
</ul>

Below we can see the phases a file goes through and the commands used to change a files state:

![git-phases](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdvzhwvr8pqc7s7p5zs4.gif)

As shown above, we have another git command that puts our files in the staged status, meaning our files are now ready to be committed. To add files to the staged area we use the command: <strong>git add</strong>
The command is followed by the file/files you want to add to the staged status.
For example: <strong>git add index.html style.css</strong>

Once we stage our files we can check the status of the files by using the command: <strong>git status</strong>
We can also remove the files from the staged status by using the command: <strong>git rm --cached</strong>
This command is followed by the name of the file you want to remove from the staged status.
For example: <strong>git rm --cached index.html</strong>

![git-status-check](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/idtrppt87jyp55aofn96.gif)

We now are ready to make our first commit!
To make a commit we use the git command: <strong>git commit -m "placeholder"</strong>
The git commit command requires us to create a message describing our commit, this is what goes in-between quotation marks.

<blockquote>The message should be short and descriptive.</blockquote>

You might be surprised to see git respond with a message <strong>"Please tell me who you are"</strong> that requires you to run a few necessary commands. This is shown because git needs to know who you are, since it will track which developer made the snapshots to the files of a project.

We will first start by giving git our name.
We do this by typing the commands: <strong>git config --global user.name John</strong>
Next we will give git our email address. You can use your real email if you want to or a fictional one like we have here.
We do this by typing the commands: <strong>git config --global user.email john<span>@</span>gmail.com</strong>

![git-authentication](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t4bo1wl7iuykha1l8btc.gif)

Before we make our commit we have to check that our username and email address are correct, we can do this by using the following commands. 
To check our username we use the command: <strong>git config --global user.name</strong>
To check our email address we use the command: <strong>git config --global user.email</strong>

Finally we can perform our commit!
![git-commit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rf4eokyv8vhq1521wukp.gif)

You may think that once you perform a commit, you are storing a copy of the project. <strong>But that's not right!</strong>
Every time you make a commit, git basically takes a picture of what your files look like at a specific moment and it stores a reference to that snapshot. This a one of the advantages of using git.

We have covered the phases or cycle a file goes through when using git, we have given our name and email address to git so that it knows who we are and we performed our first commit, I think it's time to move on...

<h2>Check History</h2>
Checking the history of our project is a valuable asset especially as time goes on, going back in time to see your previous work has great uses for developers.

Imagine you are working on a project and you have tons of files to modify, when you use the <strong>git add</strong> command followed by the file/files you need to add. It can be tedious to add each file name especially if you got 20+ files to stage. Another way to add all the files you inserted and modified at once is the command: <strong>git add .</strong>. Adding the . at the end, will select all files that have been inserted or modified.

After we've added new files and made modifications to existing ones. We need to prepare them for a commit by using <strong>git add</strong>. Once they have been committed we can check the history of our commits by using the command: <strong>git log</strong>

![git-log](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/frt6az4f91q4h152fecd.gif)

The output of the log shows us our commits we've made. The first thing to notice is the string of weird characters next to the word commit. These weird characters are actually the unique identifier of this specific commit.
Next we see the (Head -> master), which is a sort of pointer, but this will be discussed later...
Below that we see the Author, which is the person who made the commit followed by the <em>date and time</em> the commit was made.
Lastly we see the message of the particular commit.

But lets say you have made a lot of commits and you want to see them all, having this long output can make it difficult to navigate, this is why there is another way which summarises the logs returned from using <strong>git log</strong>
The command which will give us a shorter output is: <strong>git log --oneline</strong>

![git-log-short](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m9u3fg64h997rhj12okx.gif)

This is surely a better output than before and makes it easier to navigate and make sense of our commits we've made.

This concludes this chapter on Git and Github. I hope you enjoyed it.

![phew](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yqlnh8gjmc4kj1r99kzg.gif)