Learning Git

Task 2.2) Learn Git

In the last blog we went through the entire installation process of setting up the development environment . In this blog we will learn about Git and the commands associated with it.Lets get started :-) !!

Git

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 is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.

GitHub???

GitHub is a Git repository hosting service, but it adds many of its own features. While Git is a command line tool, GitHub provides a Web-based graphical interface. It also provides access control and several collaboration features, such as a wikis and basic task management tools for every project.

These three features — fork, pull request and merge — are what make GitHub so powerful.

Why to use Git???

Free and open source

Git is released under GPL’s open source license. It is available freely over the internet. You can use Git to manage property projects without paying a single penny. As it is an open source, you can download its source code and also perform changes according to your requirements.

Fast and small

As most of the operations are performed locally, it gives a huge benefit in terms of speed. Git does not rely on the central server; that is why, there is no need to interact with the remote server for every operation. The core part of Git is written in C, which avoids runtime overheads associated with other high-level languages.

Implicit backup

The chances of losing data are very rare when there are multiple copies of it. Data present on any client side mirrors the repository, hence it can be used in the event of a crash or disk corruption.

Security

Git uses a common cryptographic hash function called secure hash function (SHA1), to name and identify objects within its database. Every file and commit is check-summed and retrieved by its checksum at the time of checkout.

No need of powerful hardware

In case of CVCS, the central server needs to be powerful enough to serve requests of the entire team. For smaller teams, it is not an issue, but as the team size grows, the hardware limitations of the server can be a performance bottleneck.

Easier branching

CVCS uses cheap copy mechanism, If we create a new branch, it will copy all the codes to the new branch, so it is time-consuming and not efficient. Also, deletion and merging of branches in CVCS is complicated and time-consuming. But branch management with Git is very simple. It takes only a few seconds to create, delete, and merge branches.

Let us see the basic workflow of Git.

Step 1 : You modify a file from the working directory.

Step 2 : You add these files to the staging area.

Step 3 : You perform commit operation that moves the files from the staging area. After push operation, it stores the changes permanently to the Git repository.

How to install Git???

Install Git on any OS(Mac/Windows/Linux)

How to use Git???

Once you have installed the Git, let’s understand the commands used to access Git!!!

For setting the user name and email id , use the following commands

$ git config --global user.name "Sayali"
$ git config --global user.email your@example.com

The global keyword refers to the current user.If we want to use a different name,don’t use the global keyword.

If you want to check your settings, you can use the following command to list all the settings.

git config --list

If you ever need help while using Git, there are three ways to get the manual page (manpage) help for any of the Git commands:

$ git help <verb>
$ git <verb> --help
$ man git-<verb>
eg $ git help config

To start working with git, you first need to initialise it in the current directory. For this purpose, you may use the following command :

$ git init

For adding all files to the staging area,use the following command:

$ git add *

For adding individual files,use the same command but with the filename as follows:

$ git add <filename>
$ git add home.html

This is the first step in the basic git workflow. To actually commit these changes use the following command:

$ git commit -m "Commit message"

For viewing all the commits which have been executed,use the below command:

$ git log

For comparing the git repository with the working project and checking for any changes use the following command:

$ git status

To check the differences between local copy and repository file :

$ git diff

To check differences between staging area file and repository :

git diff — staged

To delete a local file, execute the command :

$ git rm <filename>
eg $ git rm home.html

Make sure you commit your changes to delete the file from repository.

In certain cases, you may want rename a file. For this purpose just run the command :

Eg: git mv index.html home.html

For removing a file from staging area, just execute the command :

git reset HEAD home.html

Now, let us move on to some GitHub commands.

git remote add nickname_of_repo URL_of_repo
Eg :git remote add origin https://github.com/username/myproject.git

Git now knows there’s a remote repository and it’s where you want your local repository changes to go. To confirm, type this to check:

git remote

To push the commit changes to remote repository :

git push -u origin master

The name of our remote is origin and the default local branch name is master.The -u tells Git to remember the parameters, so that next time we can simply run git push and Git will know what to do

To pull the changes from remote repository branch to local copy, use the command :

git pull origin master

If you want to get a copy of an existing Git repository — for example, a project you’d like to contribute to, use the command :

git clone [url]

You may have seen that every time we make a ‘git push’, we are prompted to enter our username and password. Instead we could use a SSH key for authentication.

Generate an SSH key

SSH (Secure Shell) is a network protocol that allows secure remote access over an encrypted connection. Through an SSH connection you can easily manage your files and folders, modify their permissions, edit files directly on the server, etc. SSH provides several types of authentication, including password and key-based authentication. Key-based authentication is a very secure authentication method and it is recommended over password-based authentication which can be brute-forced.

Windows

PuTTY is an SSH client for Windows that you will use to generate your SSH keys. You can download PuTTY for free from this URL.

To generate a key with PuTTY , you should:

To generate an SSH key with PuTTYgen, follow these steps:

  1. Open the PuTTYgen program.
  2. For Type of key to generate, select SSH-2 RSA.
  3. Click the Generate button.
  4. Move your mouse in the area below the progress bar. When the progress bar is full, PuTTYgen generates your key pair.
  5. Type a passphrase in the Key passphrase field. Type the same passphrase in the Confirm passphrase field. You can use a key without a passphrase, but this is not recommended.
  6. Click the Save private key button to save the private key.You must save the private key. You will need it to connect to your machine.
  7. Right-click in the text field labeled Public key for pasting into OpenSSH authorized_keys file and choose Select All.
  8. Right-click again in the same text field and choose Copy.

Uploading an SSH key

Now you need to upload the copied SSH key to your Cloud Management portal.

  1. After you copy the SSH key to the clipboard, return to the Cloud Management portal.
  2. In the SSH Key field, paste your SSH key.
  3. In the Name field, provide a name for the key.Providing a key name is optional but is a good practice for ease of management.
  4. Click the Add this key button.

Linux

You can generate an SSH key pair directly in cPanel, or you can generate the keys yourself and just upload the public one in cPanel to use with your hosting account.

When generating SSH keys yourself under Linux, you can use the ssh-keygen command. To do so follow these steps:

  • Open up the Terminal
  • Type in the following command:
ssh-keygen -t rsa
  • Next you will have to type in the location of the file where you would like to save the private key.
Enter file in which to save the key (/home/youruser/.ssh/id_rsa):

The public key will be saved in the same location, under the same file name, but with the .pub extension. Type in nothing to use the default location, which is /home/youruser/.ssh/id_rsa.

  • Finally you will have to type in a password. This will be the password required to load the private key and use it to connect via SSH later on:
Enter passphrase (empty for no passphrase):

The entire key pair generation process would look like this:

user@localhost: ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/youruser/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa.
Your public key has been saved in id_rsa.pub.
The key fingerprint is:
16:8e:e8:f2:1d:c9:b9:cf:43:9a:b3:3c:c1:1f:95:93 user@localhost

This will create a private key written to /home/youruser/.ssh/id_rsa and a public key written to /home/youruser/.ssh/id_rsa.pub.

To use the keys with your SiteGround account, you need to upload the public key in cPanel, under SSH/Shell access. The private key needs to be loaded in your Terminal using the ssh-add command:

ssh-add /home/youruser/.ssh/id_rsa

After which you will be asked to enter the passphrase of your key.

That’s all for this blog,we will go through the installation process of Hasura Local Development. Stay tuned for the next blog.Till then Ciao!!!