Git, GitHub and Version control

Version control

Remember the “checkpoints” we used to have in our games so that the progress made by us before a certain checkpoint is saved and we may continue our game from any of the saved checkpoints after the game is over? In coding, we often have to make certain amendments in our code in order to update and optimize it. To do so, we use version control so that we have a record of the amendments made and we may change our amendments from any of the “checkpoints”, here called commits.

Git

Git is an open source version control system which not only allows us to improve our codes after the project is deployed but also make our contributions in other open source projects. We can just search the projects we want to work on, fork them, clone them, make our changes and request the author via a pull request to merge our changes in the original project.

GitHub

It is a website which allows us to use git and version control.

We can perform version control by two methods.

  1. By using a terminal.(I will be using Hyper Terminal)
  2. Via GitHub website.

Let’s now see how to use Git and version control. Note that I am using Hyper terminal to do all the tasks.

Before we move further:

  1. Create a new directory say, Story.
  2. Create a new file called say, file1.txt.

3. Add some text in file1.txt .

To track changes made in the files, initialise git repository.

git init

Doing so will create a hidden .git file which can be viewed by typing ls -a in the Hyper terminal. This file will perform version control and will track and commit the changes that we make in our file(s).

In order to track the changes we make in our files, we must add them in staging area. Staging area is an intermediate place where we pick to choose the files we want to commit. To see what is in the staging area, use

git status

Here we can see that file1.txt is untracked at the moment and hence is in red color. In order to start tracking changes in it, use

git add <file name>

We can see by typing git status that the string file1.txt has become green and hence it is in the staging area now, has become trackable and is ready to be committed.

git commit -m “ complete file1”

The message in the quotation marks(here “complete file1”)should be as explicit as possible as it signifies the changes that we have made in the file between the last commit and the present commit and it will help us to recall the contents of the file. -m is used to specify a commit message.

To see the commits we’ve made, use

git log

Let’s create a few more files say file2.txt and file3.txt .

Adding some texts in both of the files.

To both the files together to the staging area, use

git add .

Committing both the files.

git commit -m “complete file1 and file2”

Seeing both the commits

Now suppose I changed my file2.txt and saved it.

If we check the status at this time, we will find that we have modified file2.txt

To see the difference between file2.txt before and after making changes, type

git diff file2.txt

Here we can find the content of the file before(red color) and after(green color) changing the text.

To reset the content of file2.txt to the previous version, type

git checkout file2.txt

file2.txt restored to the previous version

Creating public repositories

The repositories which are hosted on someone else’s server by using GitHub are called public repositories.

Making your repository public means that everyone can see it and this can be achieved with GitHub.

We can push our private repositories to GitHub to make it public.

To push your repository, type

git remote add origin <your GitHub repository address>
git push -u origin master

The first line tells the local repository that we have made a remote repository over the internet and want to send our entire commits there. .origin is the name of our remote and can be customized.

The second line of code pushes our local repository to the remote repository using the u flag. We are going to push it to a remote named origin and to the branch named master. The master branch is the default branch of all of our commits.

Our files are now hosted on GitHub.

Gitignore

While uploading all our files to the remote repository, we surely don’t want something that should not be uploaded(say, API keys). To do so, we have something called as Gitignore. We have to add the a file named .gitignore and add the filenames in it that we don’t want to upload.

Adding the files which i don’t want to upload.

Note that secrets.txt is the file which I have created in my working directory but don’t want to push. Now as usual, go and add the file(s) you want to upload, commit them and then push them. All the files except those listed in the .gitignore file will be pushed to the remote repository.

Thanks for reading. If you liked the blog, give it a 👏

Would love to hear from you at saadahmad728@hotmail.com

--

--

--

A web developer, mechanical engineer and an eternal learner.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Personal CRM — Meaningful Relationships

How the Helper Methods in Sinatra Helped Me Made My App DRY

Why Code Comments Are Important?

MySQL CREATE TABLE in PHPMyAdmin

Filtering Laravel Eloquent Models Using Osmose Package

Poker Po Polsku Online

Poker Po Polsku Online

Voxel to Mesh Conversion: Marching Cube Algorithm

My interview questions

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
SAAD ZAKAULLAH AHMAD

SAAD ZAKAULLAH AHMAD

A web developer, mechanical engineer and an eternal learner.

More from Medium

Git Theory And Branch concept

Git Guide 01

Beginners Guide to Git & Github.

Get Rid of Git’s TLS Certificate Verification Warning