Empowering your business

SAI Express : A Training Journey, A Happy Journey ? Halt 1 - Git

Namashkar everyone. I hope you all liked my previous blog post on Authentication Protocol : OpenID, OpenID 2.0 and OpenID Connect. Now, I would like to share my experience about SAI (Sai Ashirwad Informatia) Express. Confused!! Give me a minute. Before that, where’s Bob? Hey Bob, you there??

Bob : Hi Sreenesh, I was wondering about a journey.

Me : Journey? Where you going Bob?

Bob : I am going to join Sai Ashirwad Informatia next month. So, I am going by SAI Express by end of this month.

Me : Bingo. Bob, even I am at SAI. You gonna have a great experience there. Trust me.

Bob : Very Excited.

Me : Let me double your excitement.

Bob : How?

Me : I am going to tell you about my 7 days journey in SAI Express one by one. Today, I am gonna share my first day experience.

Bob : Wow. Let’s start.

Me : Here we go.

Journey at each halt (station stop) was astonishing and productive. Consider each day of a week as a halt. So, let me explain you how each of them build on happiness, creativity and value-added learning experience to my journey step-by-step.

Friday 16, January, 2015 (Halt 1)

Extremely electrified and thrilled, I started my journey in SAI Express at 10: 00 am. I was not the only voyager, we were six in all. Kick-start with a great seminar on Developments and Achievements in SAI, Introduction from our side, followed by delicious snacks for everyone, I was sure this is the place where I can fulfill my dreams.

Bob : Wow, sounds interesting. Tell me more.

Me : OK. Have you heard about ‘Git’?

Bob : Geet!! Which Song? (Geet means song in Hindi)

Me : Lolz. Bob, its not Geet. Its Git. We had a great session on Git.

Git is a

Free Open source Distributed Version Control System Bob : What is Version Control System?

Me : It is a repository (a storage) of files, especially code files (computer programs). Git can be used to manage files, keep track of changes made to the files, reverse the changes made to the files, and much more. It also keeps track of who made the changes. We can manage different versions of files for a project which is very important while tracking the amount of work done.

Bob : Great. So, How can I use a Git?

Me : To make use of git repository, you can make use of either :

Gitlab Github etc…

Bob : OK. So what are hubs and labs doing in git?

Me : Hahaha. Bob, take it easy. Let me explain.

Gitlab :

Free service Used by organizations for maintenance of git repositories internally, i.e. organizations can setup there own Gitlab servers for their projects However, individuals can create their own account also. For account creation and use, you can go here. Github :

Free service again Source codes that you put are publicly available (Exception : If you have paid account then, it will not be publicly available for others) To join Github, you can go here. Bob : OK. So, which one I should go for?

Me : It completely depends on you. If you are a firm or company, then on internal basis, Gitlab can be used for code management. However, Github can be the choice if you wanna publicize your code repository. By the way, you are going for SAI Express, so very soon you will be working on Gitlab.

Bob : Wow. Very exited now. Though I am going to learn about Git in detail at SAI Express, can you please tell me something more about Git so that I can standout among my commuters in my SAI Journey.

Me : Hahaha….So, you wanna shine among your commuters……

Bob : Please…..I want to learn.

Me : OK. Now, only making an account over Gitlab is not enough, when you create a project on your desktop PC, you need to push and pull the project files to the git repository so that same copy/updated copy is maintained on both, your desktop PC and your git project.

Bob : Push!!! Pull!!! Whats this?

Me : Bob, you do one thing. Remember the following instructions I am going to say, it will clear all your doubts.

Bob : OK.

Steps to be followed

Go to Gitlab and create your account Capture2

Once the account creation is successful, you need to confirm your account by clicking on a link which is send to your registered email account. Then, you will be redirected to your Dashboard (Work Area over Gitlab) or you can login from here Sign In Panel Capture3

This is how the Dashboard appears (Shown below) Capture4

Now, you can see three main areas, which you can look upon. It is well explained by them. Capture5

I will be now focusing on creating new projects. Click on the “New Project” tab. Something like this (shown below) will open, where you need to fill in the details for new project. I have created a “hello-world” sample project. Note that, I have used “-” in separating “hello world”. This is a good practice. Avoid underscores(_) in project names. You can keep the access level as “Private”, “Internal” or “Public” as per your need. I have kept it “Private” here. Click on “Create Project” Capture6

Now, you please see the red marked sentence “You won’t be able to pull or push project code via SSH until you add an SSH key to your profile” (below). So, now you need to create a SSH key. Capture7

Bob : Whats SSH key?

Me : SSH stands for “Secure Shell” or “Secure Socket Shell”. SSH keys are used to identify and get access to remote/trusted computers. It is generated using various cryptography algorithms.

Bob : Can you please explain me in simple words?

Me : OK. In simple words, to grant access to your project files from desktop PC to git and vice-versa safely, you need an encryption which is done using an SSH key.

Bob : I am confused. I am maintaining my project on my PC then what is the need for putting it to Gitlab?

Me : Good Question. When you create a local git for a project on Desktop PC, the different versions for that file is maintained on local git too, but then to get those versions, we need to write ample of commands on terminal which would be a tedious task. Instead, Gitlab provides you with a very good GUI that will help to to retain the history (versions) very easily. Also, it provides value added features.

So, using Git, you get following benefits :

Push (Send) the changes made to the file, to the Gitlab Pull (Receive) the latest updated file to the desktop Can prepare “Wiki” documents (Documentation related to your project) and many more features…

Bob : Very good.

Me : Also, important thing to note is that, Push is NOT “Upload” and Pull is NOT “Download”. Push and Pull operations manage the consistency among the files on the Gitlab and Desktop PC. They help to keep the files upto date on both, Gitlab and Desktop PC.

Bob : OK.

Me : Now, you need to create SSH key, for this you need to download a Git Bash for Desktop PC, i.e. a command-line interface + a GUI interface to carry on git operations to the Gitlab. You can download it from here for appropriate OS and then install it.

Bob : OK. Whats next?

Me : We are now gonna create a local git and then an SSH key.

Open the git-bash command line interface cmd

Bob : OK. I have one question. What is that “master”?

Me : Good observation Bob. It is called as branch. We can say, there are 3 branches that we can work upon.

Dev-Stage : Initial Development branch (work area) where you start pushing your files to, during your project development Int-Dev Integrated Stage : Second branch (work area) used for pushing the code files for testing (error and trial). However, you can skip this if you consider testing your files in initial stage. Master : Default branch (work area) where you actually push your final code files (files that you consider perfectly coded and final for your project). Though, it is not necessary to go through each of these branches, it is very good industrial practice to build your project by taking your project through all three branches. However, you can directly switch to “master” branch too for all operations.

Bob : OK.

Me : Now, perform the following steps :

Create a project folder. I have created a simple folder named “helloworld” in my Xampp. Then, I create a PHP file “hello_world.php” in this folder and write the simple code to display “Hello World” hw

(Please note that I am not gonna run this file. I am gonna show only the git operations. You can create file using any programming language you want)

Here below, we have traversed to the folder containing our php file i.e. “helloworld”. There, you need to perform the very first git operation : “git init” which initializes an empty local git repository which is very important for us to collaborate with the Gitlab. step1

Now, go to your Gitlab account and see for something like this (below): step3

Write the above commands one by one in your Gitbash terminal. These are the global setup you need to do once and only once when you are going to use this git service. It is like a small talk between your Desktop PC and local git discussing your username and email for identifying you. step2

Now, next important step is to create a remote (handle) to your Gitlab project “hello-world” by writing the handle provided here (This is available in your project) to your command line (below). Please note that this address “git@gitlab.com:sree_nesh/hello-world.git” should be properly written while creating the remote. step4

step5

In above command : remote – command for invoking remote (handle) add – to add remote origin – name of the remote (can be anything) URL – git@gitlab.com:sree_nesh/hello-world.git – unique address representing your project on Gitlab If you perform any mistake in creating remote handle, you can do one of the following :

Remove the remote completely using following command. Here, I am removing “origin” git remote remove origin

If you have mentioned wrong URL, then you can correct it using following command. Suppose, I had created a remote named “origin” with a wrong URL and I need to change it, then I will write : git remote set-url origin new-url

And then create new handle again for same project. Thus, for each new project :

Create a new handle. Always good to keep one handle per project.

Bob : Sreenesh. I didn’t get this!!!

Me : Relax.

Example

Me : You have two Television sets at your home, right?

Bob : Ya.

Me : How do you control your Television set, I mean to say how do you change channels, increase/decrease volume,etc…?

Bob : Of course using Remote Control.

Me : So, do you use same remote control for both the television sets?

Bob : Are you mad Sreenesh?? Why will I use same remote control for 2 different television sets. I use two different remote controls.

Me : Bingo, you just gave answer to your previous question.

Television Set 1 = Project 1 [Remote 1 for this] Television Set 2 = Project 2 [Remote 2 for this] So, to control your Project operations (like Push and Pull) onto Git, you need different remotes.

Bob : Amazing Sreenesh. What an example. Got it.

Me : Fantastic. Then, I shall continue.

Now, its time to create SSH key. Command used to create SSH key is ssh-keygen -t rsa

When you run this command, following occurs step6

It is actually generating a public/private rsa key pair using the RSA algorithm. It asks to enter the file to save key. Don’t specify any file. It will take the default path and create a file to store the key as “(/c/Users/sreenesh/.ssh/id_rsa)”. Your default path may be different. Press enter to continue the key generation.

step7

In above command line interface, after pressing the Enter, it asks for “passphrase” which is like providing a “password”. Please note that while typing passphrase, it will not be visible to you, so type them correct and remember this passphrase as we will be needing it later. step8

When you pressed Enter after filling on the passphrase, a SSH is generated with the key’s randomart image (as shown above)(Randomart image is used by humans to compare the 2 keys). I will come to “Randomart Image” in any of my coming blogs. for the timing, consider it as “a fingerprint generated that helps humans to visually compare keys) Now, to see the key that is generated, write the following command cat ~/.ssh/id_rsa.pub

Here, cat – to show the key ~/.ssh/id_rsa.pub – takes you the folder “.ssh” in which key is stored in file “id_rsa.pub” Thus, key is shown as follows : step9

Bob : My God!!!! This key is very huge.

Me : Hahaha. Yes. It does safe encryption.

Then copy this key and paste it in the Gitlab by visiting the link “add an SSH key” on Gitlab as shown below : step10

You will be asked to fill in the title and paste the key as follows : step12

Now, sometimes it may happen that when you paste the key in the “Key” field and press “Add key”, it may give error. This is because the pattern of the key that we have pasted here changes which actually doesn’t match the key fingerprint. Bob : That means, we can’t do anything now??

Me : There is a solution to this. When you installed “Git” on your Desktop PC, two things were installed :

Git Command-Line Interface Bash Git GUI Bash Up till now, we were using command line interface to perform all git related stuff. But one interesting thing is that we can perform all the above operations using Git GUI, which is very faster (no need to write commands here). However, I would recommend to use command-line interface as it will help to learn commands.

So, we will be using the Git GUI Bash to copy the key and paste it on Gitlab. When you open your Git GUI Bash, you can get your SSH key in the “Help Tab” –> “Show SSH Key”. Then, you will get something like this : step14

Copy the key by doing “Copy to Clipboard” and paste it into “Key” field in Gitlab as follows : step15

And when you press the “Add key” button, the SSH key gets added to your account as follows : step16

You can now perform push and pull operations very easily and safely.But, before that you need to perform two final steps : Git Add – It stages your files i.e. prepares it for proper commit git add filename1, filename2 (stages selected files, which are not yet staged)

OR

git add . (stages all the files in that directory, which are not yet staged)

Git Commit – Commits the file for push operation git commit -m “hello world pushed to git first time”

Here, -m is for “providing the commit message”. This message will get appended when you push the file. (See below)

step17

So, it shows that commit is done successfully, now I am going to push my file hello_world.php to Gitlab server using following command : git push origin branch_name

Here,

push – operation we are going to perform origin – remote name branch_name -name of the branch (Master/Dev-stage/Int-Dev Integrated) (I am using my default branch as “master”) step21

Above, it is asking for the passphrase (the password you gave during key creation). Write the passphrase and press “Enter key” step22

On successful connection with the Git, the file will be pushed successfully (See above). Please note that once you write the push command, you need to enter your passphrase immediately, else the connection to git will be closed and again you need to write the “push” command. step23

Above, you can see that “sreenesh pushed new branch master at sreenesh/hello-world” meaning, the file has been pushed successfully. You can now see the files here (See below) : step24

Similarly “Pull” can be used to pull the files down to your local git using command git pull origin master

Bob : Yooo. This was really mind blowing concept. The first day in SAI Express was awesome Sreenesh. SAI Rocks. I am now excited for the Second Halt i.e. Day 2.

Me : Hahaha….Bob, lets stop here today. You try out the things and then we will move on to next part of SAI Express. You have ample amount of time to learn next part.

Bob : OK. Great.

Me : So, in short, lets revise the steps for Git :

Step 1 : Create an account to Gitlab (one time process)

Step 2: Create a project in the Gitlab (create “New Project” for each new project)

Step 3: Download git for local desktop and install it (one time process)

Step 4: Use command line or GUI for following steps :

  1. Create an SSH key (Though we can create multiple keys, it is good to maintain one key, one time process)
  2. Copy and paste the SSH key to Gitlab’s “add SSH Key” area (one time process)
  3. Go to the folder that contains project on desktop (Must create a project on desktop PC)
  4. Initialize a git (git init) (one time process per project)
  5. Add remote to the project (one time process per project)
  6. Perform following steps each time you make changes to the files in your project :
    1. Staging of files (git add . )
    2. Commit your files (git commit -m “your_message” )
    3. Push your files (git push remote_name branch_name)

Me : Thats all my brother Bob.

Bob : Thank you so much for this. I will try out this and will see you next time for next for mind blowing concepts in SAI Express

I hope you liked this post too. Please do comment and feel free to ask any queries. Till then, me and Bob signing off.

Subscribe

Stay updated with our newsletter, we occasionally do write about technology, open source and best practices.
Don't worry, we will never share your details and won't spam you either.